isClockwise...?

Community Forums/General Help/isClockwise...?

jfk EO-11110(Posted 2016) [#1]
Imagine 3 random coords on a 2d screen, coord 1,2 and 3, assembling a triangle.

What would you say is an elegant and fast way of determing whether the coors are aligned clockwise or counterclockwise?

Thank you.

btw. I already have one solution, but it seems bulky, slow and in fact "unelegant" in my opinion:

Find out which coord is the middle one on the x axis. Then check whether both other two coords are eigther y-below it or y-above it. In that case it is simply testing which has lower X. But when one of the two others is y-below and the other is y-above the middle coord then I use sswifts' Lines-intersect code to determine whether the line between the outer coords passes left or right to the middle coord. So, no need to post this very solution.


Floyd(Posted 2016) [#2]
You have points P1, P2, P3 all in the x-y plane.

Subtract P1 from P2 to get the vector from P1 to P2. Call it (a,b).
Subtract P1 from P3 to get the vector from P1 to P3. Call it (c,d).

If a*d - b*c is positive then points are in clockwise order.

NOTE: I originally wrote negative, forgetting that the y-axis points downward in 2d.
If you are really working in 3d so y points up then negative means clockwise.
Just try a trivial example with points (0,0) (1,0) (0,1).

This amounts to checking the z-component of the cross product.


jfk EO-11110(Posted 2016) [#3]
Thanks a lot, that is precisely what I meant by "elegant".