I come to ya'll once again for a little help with tweaking my code. I was working on making a routine to make the list of vertex that I have, run clockwise if they are not listed that way already. My algorithm is pretty simple: 1) find the largest y-value, 2) compare the next vertex's x-value to that of the largest y-value, 3) roughly, if the difference is positive, then it is clockwise, otherwise reverse the order. Now my code works for most cases, yet it does wrong on a case like:
| ^ | / / | / | | --- ---------
the list comes in going clockwise, but my algorithm changes it because the next x-value after the greatest y-value is less than the x-value of the greatest y-value. Any help would be much appreciated. Thanks
my ($highest, $hv, $xclock, $negx); my @backwards; # MAKE CLOCKWISE ************************************* CLK: for $i (0..$#blocks) { $highest = 0; for $j (0..$#{$blocks[$i]}) { if ($blocks[$i][$j][2] > $highest) { $highest = $blocks[$i][$j][2]; $hv = $j; } } if ($hv == 0) { print "zero [$i][$i][$i][$i][$i]\n"; $xclock = $blocks[$i][($hv + 1)][1] - $blocks[$i][$hv][1]; if ($xclock == 0) { $xclock = $blocks[$i][($hv + 2)][1] - $blocks[$i][$hv][1]; } $negx = $blocks[$i][$hv][1] - $blocks[$i][($#{$blocks[$i]} - 1 +)][1]; print "00000 blk [$i][$hv] clkwise [$xclock], [$negx]\n"; if ($xclock == 0) { print "xclock 0 \n"; if ($negx < 0) { print "change xclock 000\n"; @backwards = reverse @{$blocks[$i]}; for $k (0..$#backwards) { print "xcl0000 [$k][1] $backwards[$k][1]\n"; print "xcl0000 [$k][2] $backwards[$k][2]\n"; } @{$blocks[$i]} = @backwards; next CLK; } } if (($xclock < 0) ) { print "change 000\n"; @backwards = reverse @{$blocks[$i]}; for $k (0..$#backwards) { print "0000 [$k][1] $backwards[$k][1]\n"; print "0000 [$k][2] $backwards[$k][2]\n"; } @{$blocks[$i]} = @backwards; next CLK; } } $xclock = $blocks[$i][($hv + 1)][1] - $blocks[$i][$hv][1]; if ($xclock == 0) { $xclock = $blocks[$i][($hv + 2)][1] - $blocks[$i][$hv][1]; } $negx = $blocks[$i][$hv][1] - $blocks[$i][($hv - 1)][1]; print "BBBBBBBBBB [$i][$i][$i]\n"; print "blk [$i][$hv] clkwise [$xclock], [$negx]\n"; if (($xclock < 0) || ($negx < 0)) { print "change reg\n"; @backwards = reverse @{$blocks[$i]}; for $k (0..$#backwards) { print "[$k][1] $backwards[$k][1]\n"; print "[$k][2] $backwards[$k][2]\n"; } @{$blocks[$i]} = @backwards; next CLK; } }

In reply to 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.