Question

I have seen similar questions on here but just cannot get the math to work.

I have a circle, with two known points on the circumference (x1,y1,x2,y2) with the center of the circle as cx,cy

If I am stood at cx,cy and looking at point x1,y1 how can I tell which way I need to turn to face x2,y2?

So far I am working out the angle to each point

Atan((cx-x1) / (cy-y1)) Atan((cx-x2) / (cy-y2))

I have then tried a simple subtraction, using mod to ensure both are between -2pi and 2pi but I just get some strange answers. It appears the odd results occur when the two points lie above and below the horizontal line drawn through the center point.

But ill be honest I have tried so many things now my head hurts! It does not have to be a computationally fast solution as its only done once. Thanks in advance.

Was it helpful?

Solution

The answer is given to you by the sign of (x1-cx)(y2-cy) - (y1-cy)(x2-cx).

Proof:

Let A be the direction from C to (x1,y1), expressed as an angle measured anticlockwise from the X axis; B be the direction from C to (x2,y2), expressed the same way; and r be the radius of the circle. Then (x2,y2) is to the right of (x1,y1), as seen from C, if A-B lies between 0 and pi or between -2pi and -pi (that is, if sin(A-B) is positive), and to the left if A-B lies between -pi and 0 or between pi and 2pi (that is, if sin(A-B) is negative).

Now,

(x1,y1)=(Cx + r cos A, Cy + r sin A) 
(x2,y2)=(Cx + r cos B, Cy + r sin B)

So

  (x1-Cx)(y2-Cy) - (y1-Cy)(x2-Cy) 
= (r cos A)(r sin B) - (r sin A)(r cos B)    
= - r^2 (sin A cos B - cos A sin B)
= - r^2 (sin (A-B))

which has the opposite sign to sin (A-B).

OTHER TIPS

Let's say A1 is the angle between the vector from (cx, cy) to (x1, y1) and the horizontal axis, and A2 is the angle between the vector from (cx, cy) to (x2, y2) and the horizontal axis. When you sit at (cx, cy) and look at the point (x1, y1), the point (x2, y2) is on your right if and only if the angle between the two vectors is less than pi and on your left if and only if the angle is more than pi.

Since the sine of a positive angle is positive from 0 to pi and negative from pi to 2*pi, then it follows that the point is on your right iff sin(A2-A1) > 0 and on your left iff sin(A2-A1) < 0.

If we use the usual trigonometric identity, we have that

sin(A2-A1) = sin(A2) * cos(A1) - sin(A1) * cos(A2)

Then you just have to replace the sine and cosine by their formula with the cartesian coordinates. The denominator is factored out because the points sit on a circle.

Therefore the sign of sin(A2-A1) is the same as the sign of (x2-cx)*(y1-cy) - (x1-cx)*(y2-cy).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top