Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:

I am having some points in order say 1-10 which are creating a contour(polygon) in 2-D plane. Now I want to check that these points from startPoint to endPoint are moving in clockwise or anticlockwise direction to create the polygon. Any efficient way to check this out. This polygon can be on any where in X-Y plane and may be rotated by some angle.

Posted
Updated 27-May-17 23:51pm
v2

1 solution

You will need at least 3 points to detect a direction of contour. Create a ray using the first 2 points (by connecting them, with the origin of the ray being the first point). If the third point is to the "left" of the ray, the direction of the contour is counterclockwise. If the third point is the the "right" of the ray, the direction of the contour is clockwise.

You can check if the third point is to the left or right of the ray by checking the slope of the ray then seeing if the point is above or below the point on the ray that has the same x-coordinate as the third point. One gotcha to look out for is if the slope of the ray is infinity (i.e., a vertical line). In this special case, all you need to do is check if the point is to the left or right of the ray's x-coordinate. If the slope is zero (i.e., a horizontal line), you just check if the third point is above or below the ray's y-coordinate.

Just a quick tip, because I know this basic of math can slip easily once you've had higher maths, slope is calculated by "rise over run" (i.e., y-coordinate differences between two points, calculated by x-coordinate differences between those same two points). And you'll probably be making good use of the good old "y = mx + b" slope-intercept equation.

Oh, and one more thing. If you have a math library you can use, the cross product can be used to calculate whether the 3 points are in a clockwise or counterclockwise orientation. This is a common technique in 3D rendering. Just make a triangle out of 3 of the points. You can read more about this technique: here. Using this technique should speed up the calculation, if that is important.
 
Share this answer
 
v3
Comments
lynx001 27-Dec-13 1:55am    
This is true only for a strictly convex polygon, I think.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900