You just need to compare the X values between $hv-1 and $hv+1 instead of between $hv and $hv+1. That is, you don't care whether the line segment that starts at the "top" goes to the left or the right of vertical, but instead whether it goes to the left or right of the line segment that ends at the "top".
[ Update: No, that won't work quite right either. If the right-of-top line segment is longer than the left-of-top line segment then the right-of-top could still end with an X coordinate that is to the left of the left-of-top line segments X coordinate. As Thelonius says, you want to compare slopes. You can probably reduce the equations so that you don't have to worry about dividing by zero and testing for multiple cases. Let me think about that...
Here is what I came up with:
]my $b = $blocks[$i]; my $pv = $hv - 1; $pv = $#$b if $pv < 0; my $nv = $hv + 1; $nv = 0 if $#$b < $nv; for my $v ( $pv, $hv, $nv ) { $v = $b->[$v]; } my $na= atan2( $nv->[_X] - $hv->[_X], $nv->[_Y] - $hv->[_Y] ); my $pa= atan2( $pv->[_X] - $hv->[_X], $pv->[_Y] - $hv->[_Y] ); if( $na < $pa ) { # Not clock-wise:
You could simplify your code a lot with a simple:
and then merging your two big blocks of code into one block.my $pv = $hv - 1; $pv = $#{$blocks[$i]} if $pv < 0;
You could also make your code a lot easier to understand if you did something like:
so that you could writesub _X() { 1 } sub _Y() { 2 }
(or use constant if you prefer).if ($blocks[$i][$j][_Y] > $highest) {
And after those changes, having $xclock == 0 would mean that you have two lines that lie on top of each other (at the "top" of your "polygon"), which should probably just be considered an error.
- tyeIn reply to Re: Making CLOCKWISE work (off by one)
by tye
in thread Making CLOCKWISE work
by stu96art
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |