Wow! That's a great script and fun to play with!
My algorithm assumes a normalized bounding box, that is, x1 < x2, y1 < y2. I'm not yet 100% sure that this is the full explanation, but I noticed that some of your randomly generated inputs violate that assumption and those inputs are the ones that seem to trigger the odd results. Once the non-normalized bounding boxes are disallowed, the odd behavior disappears.
The following two patches to your code will allow you to demonstrate to yourself the difference between prohibiting and allowing non-normalized bounding boxes.
# ... intro stuff...
labent($frm, 'X1', \$x1, 8);
labent($frm, 'Y1', \$y1, 8);
labent($frm, 'X2', \$x2, 8);
labent($frm, 'Y2', \$y2, 8);
labent($frm, 'Start', \$deg, 8);
labent($frm, 'Extent', \$ext, 8);
#------------------------------------
#PATCH 1
my $normal = 0;
labent($frm, 'Normal', \$normal, 8);
#------------------------------------
# ... more stuff ...
sub random_args {
$x1 = random_value(0, $cw);
$x2 = random_value(0, $cw);
$y1 = random_value(0, $ch);
$y2 = random_value(0, $ch);
$deg = random_value(0, 360);
#------------------------------------
#PATCH 2
if ($normal) {
$x2 = 2*$x1 - $x2 if $x2 < $x1;
$y2 = 2*$y1 - $y2 if $y2 < $y1;
}
#------------------------------------
}
Since Tk allows both normalized and denormalized bounding boxes it probably makes sense to write the arc end point algorithm to handle both cases as well. The following patch to my earlier algorithm will allow it to handle both normalized and denormalized boxes. It will draw correctly whether or not $normal is true or false.
sub getArcEnd {
my ($x, $y, $x2, $y2, $start, $extent) = @_;
# --------------------------------------
# PATCH
# handle denormalized bounding boxes
if ($x2 < $x) {
my $tmp = $x;
$x=$x2;
$x2=$tmp;
}
if ($y2 < $y) {
my $tmp = $y;
$y=$y2;
$y2=$tmp;
}
# --------------------------------------
# $x, $y, $x2, $y2 can be the bounding box of an
# ellipse (not just a circle) so calculate vertical
# and horizontal radius separately
my $radiusX = ($x2 - $x)/2;
my $centerX = $x + $radiusX;
my $radiusY = ($y2 - $y)/2;
my $centerY = $y + $radiusY;
# Tk expects the starting angle and length of the
# arc (extent) to be in degrees but cos and sin expect
# them in radian
my $radians = deg2rad($start + $extent);
# [ x coord of arc end point, y coord of arc end point ]
# the coordinate system for Tk::Canvas makes "down"
# positive so we need to subtract the Y component
# rather than add it.
return [ $centerX + $radiusX*cos($radians)
, $centerY - $radiusY*sin($radians) ];
}
Best, beth
Update: Struck out uncertainty. Updated with patch to demo to show difference in behavior due to normalized and unnormalized bounding boxes.
Update 2: Added version of getArcEnd() that handles both normalized and denormalized bounding boxes.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.