The cross product measures the degree of perpendicular-ness of two vectors. Imagine that each edge of your polygon is a vector in the x-y plane of a three-dimensional (3-D) xyz space. Then the cross product of two successive edges is a vector in the z-direction, (positive z-direction if the second segment is clockwise, minus z-direction if it's counter-clockwise). The magnitude of this vector is proportional to the sine of the angle between the two original edges, so it reaches a maximum when they are perpendicular, and tapers off to disappear when the edges are collinear (parallel).
So, for each vertex (point) of the polygon, calculate the cross-product magnitude of the two adjoining edges:
Using your data:
point[0] = (5, 0)
point[1] = (6, 4)
point[2] = (4, 5)
point[3] = (1, 5)
point[4] = (1, 0)
So Label the edges consecutively as
edgeA
is the segment from point0
to point1
and
edgeB
between point1
to point2
...
edgeE
is between point4
and point0
.
Then Vertex A (point0
) is between
edgeE
[From point4
to point0
]
edgeA
[From point0
to `point1'
These two edges are themselves vectors, whose x and y coordinates can be determined by subtracting the coordinates of their start and end points:
edgeE
= point0
- point4
= (1, 0) - (5, 0)
= (-4, 0)
and
edgeA
= point1
- point0
= (6, 4) - (1, 0)
= (5, 4)
and
And the cross product of these two adjoining edges is calculated using the determinant of the following matrix, which is constructed by putting the coordinates of the two vectors below the symbols representing the three coordinate axis (i
, j
, & k
). The third (zero)-valued coordinate is there because the cross product concept is a 3-D construct, and so we extend these 2-D vectors into 3-D in order to apply the cross-product:
i j k
-4 0 0
1 4 0
Given that all cross-products produce a vector perpendicular to the plane of two vectors being multiplied, the determinant of the matrix above only has a k
, (or z-axis) component.
The formula for calculating the magnitude of the k
or z-axis component is
a1*b2 - a2*b1 = -4* 4 - 0* 1
= -16
The magnitude of this value (-16
), is a measure of the sine of the angle between the 2 original vectors, multiplied by the product of the magnitudes of the 2 vectors.
Actually, another formula for its value is
A X B (Cross Product) = |A| * |B| * sin(AB)
.
So, to get back to just a measure of the angle you need to divide this value, (-16
), by the product of the magnitudes of the two vectors.
|A| * |B|
= 4 * Sqrt(17)
= 16.4924...
So the measure of sin(AB) = -16 / 16.4924
= -.97014...
This is a measure of whether the next segment after the vertex has bent to the left or right, and by how much. There is no need to take arc-sine. All we will care about is its magnitude, and of course its sign (positive or negative)!
Do this for each of the other 4 points around the closed path, and add up the values from this calculation at each vertex..
If final sum is positive, you went clockwise, negative, counterclockwise.