Method One: Green formula
The green formula reveals the relationship between the double integral of the planar area and the line integral on the closed curve .
where L + indicates a positive boundary curve along a closed area.
And by the derivation process of the green formula we know:
If L=-y here, the (1) formula is guaranteed to be constant in the area and equal to the area of the closed area.
Similarly, m=x can also guarantee (2) that the formula is constant in the area and equal to the area of the closed area.
So we only need to follow the Bentiu curve integral of the polygon, if the integral is positive, it is along the boundary curve positive direction (counterclockwise), the inverse is clockwise, and the absolute value is the polygon area.
Note: The forward direction of the boundary curve is along the boundary curve, and the single connected area is always to the left of the boundary curve. (Here the boundary curve is forward, that is, we see the counterclockwise direction)
This assumes that the polygon points in our program are (X0,Y0), (X1,y1), (x2,y2), ... (xn-1,yn-1)
Let's calculate along the point (X0,y0), (X1,y1), (x2,y2), ... (xn-1,yn-1) of the curve integral.
where for each segment segment, y take (yn + yn+1)/2, DX=XN+1-XN
D=0; for(intI=0; i<n-1; i++) {D+ =-0.5* (y[i+1]+y[i]) * (x[i+1]-x[i]);}if(d>0) Std::cout<<"counter Clockwise"<<Std::endl;ElseStd::cout<<"Clockwise"<<std::endl;
Method Two: Endpoint judgment
This method is relatively simple, traverse all points, find the x largest point Pm (the point must be on the right end of the curve "convex" section), and then take the point before and after each point Pm-1, pm+1, composition vector (PM-1,PM), (pm,pm+1). The vector fork can then be multiplied to determine the clockwise or counterclockwise.
, the "right-hand rule" is used for vector fork multiplication.
+ Represents the two vectors (pm-1,pm), (pm,pm+1) of the point Pm and the front and back two points, and the resulting vector points to the z-axis negative direction;
-Represents (PM-1,PM) x (pm,pm+1) The resulting vector pointing to the z-axis direction.
Note: This must be traversed to find the bump, otherwise if the polygon contains a concave part, and the selected point in the concave part, you will get the opposite result.
How can I tell if a closed polygon is clockwise or counterclockwise?