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:

my $pv = $hv - 1; $pv = $#{$blo­cks[$i]} if $pv < 0;
and then merging your two big blocks of code into one block.

You could also make your code a lot easier to understand if you did something like:

sub _X() { 1 } sub _Y() { 2 }
so that you could write
if ($blocks[$i][$j][_Y] > $highest) {
(or use constant if you prefer).

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.

                - tye

In reply to Re: Making CLOCKWISE work (off by one) by tye
in thread Making CLOCKWISE work by stu96art

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.