I have a problem (and a solution too). What I want to know is how to arrive at "that" solution. "That" solution is perfect and working fine for all possible combinations.

Assume that you have 3 points in the XY coordinate system: P1(x1, y1), P2(x2, y2) and P3(x3, y3).

Now, join the point P2 with P3. It will result in "ray" originating from point P2 and passing through point P3. I used word ray because I want it in only one direction, i.e. from P2 to P3.

Now, with respect to point P1, is the ray P2 --> P3 in a clockwise or counterclockwise direction?


Solution is:

use following formulae to find value of z1:

z1 = (x3 - x1)(y2 - y1) - (y3 - y1)(x2 - x1)

If z1 is positive, P2 --> P3 is clockwise. If z1 is negative, P2 --> P3 is counterclockwise. And if it is 0 the points are on the same imaginary line extending from P1.

Can someone please help me how to arrive at this solution?

有帮助吗?

解决方案

The formula you've written is very similar to the formula of cross product of two vectors. As the direction of the cross product is dependent on the CW/CCW configuration, you can easily use it for your problem.

You can construct two rays P1->P2 and P1->P3. Then you can take their cross product. If the component of the product along Z-axis is positive, then P2 and P3 is in counterclockwise order, and vice versa.

If you try to do that, the result, which would be the coefficient of k (unit vector along Z-axis) in the cross product, will be precisely the same as the answer you mentioned.

其他提示

I am not sure this works. I tried it, but it keeps giving me the same answer for both. Try these coordinates:

Start point 0, 90, 0
Center point 0, 0, 0
End point 90, 0, 0
.....
directional vector 1, 0, 0
inverse directional vector -1, 0, 0
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top