I again come to ya'll for some help. I have a list of blocks, and in each list of blocks, there is a list of verticies ( these vertex make the outline of the block) and then there is a list of values (x, y, flag1, flag2, and so on). My code below goes through the list of vertex for each block, finds the greatest y-value and then compares the x-value of the next vertex with the x-value of the current vertex and if the x-value is greater than 0 then it is going in a clockwise fashion. This works great except for when the highest point is a corner, for instance the top right corner (and lets say it is going clockwise) The highest point is (120, 75) and the next point is (108, 50). My code will mark this as counterclockwise because the next x-value is less than the current x-value.
My biggest question is how to distinguish this as clockwise. I hope that I did not confuse anyone. Here is a list of the x,y values and the order they are in, followed by the code I am using.
My goal is to make the order of the points clockwise.
<x,y>
Clockwise - my code will think this is counter-clockwise and thu reverse the order (changing what is actually clockwise into counter clockwise)
<10, 10>
<15, 30>
<45, 40>
<40, 10>
<10,10>
Also when it is counter-clockwise, it will think that it is clockwise and not change it.
<10,10>
<30,10>
<30,40>
<5,40>
<10,10>
Here is the code
@blocks[$i] is the list of blocks
@blocks[$i][$j] is the list of vertex in block
$i
$blocks[$i][$j][1] is the x-value
$blocks[$i][$j][2] is the y-value
my ($hij, $highest, $hv, $xclock, $right, $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;
$right = $blocks[$i][$j][1];
}
}
print "high [$i][$hv] $highest\n";
for $hij (0..$#{$blocks[$i]}) {
if ($blocks[$i][$hij][2] == $highest) {
if ($blocks[$i][$hij][1] > $right) {
$right = $blocks[$i][$hij][1];
$hv = $hij;
}
}
}
$xclock = $blocks[$i][$hv + 1][1] - $blocks[$i][$hv - 1][1];
$negx = $blocks[$i][$hv][1] - $blocks[$i][$hv - 1];
if ($hv == $#{$blocks[$i]}) {
$xclock = $blocks[$i][0][1] - $blocks[$i][$hv - 1][1];
}
if ($hv == 0) {
$xclock = $blocks[$i][$hv + 1][1] - $blocks[$i][$#{$blocks[$i]}][1
+];
}
print "xclock $xclock block[$i] vertex[$hv]\n";
print "negx $negx\n";
if ($xclock < 0) {
@backwards = reverse @{$blocks[$i]};
@{$blocks[$i]} = @backwards;
print "reverse\n";
next CLK;
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.