If your polygon is convex (which, IIRC, it isn't -- that would be too easy), you could just test two adjacent edges: if they form a left turn, you're going counterclockwise, if a right turn, you're going clockwise.
Here's a completely untested idea: take the vertex with the highest y coordinate in your data set. It has to be on the convex hull, right? Then it won't be part of a concave section (although one of its adjacent edges may be), and you can test the orientation of its adjacent edges to determine whether the polygon's going clockwise or counterclockwise. In (admittedly vague) code:
sub is_clockwise { # $verts and $edges are arrayrefs my ($verts, $edges) = @_; my @sorted = sort {$a->{y} > $b->{y}} @$verts; my $highest = $sorted->[0]; my ($edge_1, $edge_2) = &get_adjacent($edges, $highest); if(&right_turn($edge_1, $edge_2)) { return true; # clockwise! } else { return false; # counterclockwise? } }
This code breaks if $edge_1 and $edge_2 are collinear; it will return false (they don't form a right turn), whereas you don't really know anything about the polygon's orientation. That should be pretty straightforward to fix, though. I'll let you do it. :-)
As far as a proof of correctness goes, I don't really have one. :-( Provided that the only way the orientation test can screw up is if you do it on a concave section, it should be fairly obvious: the two edges on the highest point (or any guaranteed exterior point) are goinig to be relatively convex (they'll have the same arc as the convex hull) to the polygon, and once you have that you're golden. But if the orientation test fails in other circumstances, I dunno....
At any rate, it's cheap and easy to implement, so if it's broken you probably won't have lost that much time. :-)
--
F
o
x
t
r
o
t
U
n
i
f
o
r
m
Found a typo in this node? /msg me
The hell with paco, vote for Erudil!
In reply to Re: Clockwise or Counter-clockwise
by FoxtrotUniform
in thread Clockwise or Counter-clockwise
by stu96art
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |