How to find the orientation of three points in a two dimensional space given coordinates of those points?
This formula is used to calculate cross product of vectors q-p and q-r. You can see in Geometric Meaning section that cross product value C = A x B = |A|*|B|*Sin(Theta), where Theta is angle between these vectors (point-to-point directions). Sin(Theta) = 0 for parallel vectors, positive when Theta < 180, negative otherwise.
Example:
clockwise triplet ABC: cross product of AB and AC vectors is >0
anticlockwise triplet ACD: cross product of AC and AD is negative.
Let's have three points:
Consider that we will traverse them in order P -> Q -> R
. We must find if our traversal is clockwise, counterclockwise or if all three points are on the same line.
It is well known fact that cross product of vectors can be used to calculate their orientation in 3D space (https://math.stackexchange.com/questions/285346/why-does-cross-product-tell-us-about-clockwise-or-anti-clockwise-rotation). We can use this property to calculate traversal in 2D space by extending our points and corresponding vectors to 3D case. So, let's define vectors that correspond to chosen above direction and extend them to 3D case:
Then we calculate cross product of these vectors:
Depending on the value of Z-coordinate, original points were traversed counter-clockwise (if it is negative), clockwise (if it is positive) or they are on the same line (if value is 0).
You can also recall right-hand rule (https://en.wikipedia.org/wiki/Right-hand_rule#Cross_products) that is usually taught in elementary school during Physics course to determine vectors orientation.
Let's check!
Test case #1: Consider that we have points P = (0, 0), Q = (1, 0), R = (1, 1)
. Draw them on piece of paper draw arrows P->Q
and Q->R
. You will see that we traverse these points counter-clockwise.
Substituting into equation from above, we have:
((0 - 0) * (1 - 1) - (1 - 0) * (1 - 0)) = -2 < 0
,
so the points are oriented counter-clockwise.
Test case #2: Let's test with P = (0, 0), Q = (1, 0), R = (1, -1)
. Obviously, we traverse these points clockwise.
Substituting into equation from above, we have:
((0 - 0) * (1 - 1) - (1 - 0) * (-1 - 0)) = 2 > 0
,
so the points are oriented clockwise.
Test case #3: Finally, let's test with P = (0, 0), Q = (1, 0), R = (2, 0)
. Points are on the same line y = 0
.
Substituting into equation from above, we have:
((0 - 0) * (2 - 1) - (1 - 0) * (0 - 0)) = 0 == 0
,
so the points are on the same line.
Hope this helps!