angle in a circle

Started by
12 comments, last by alvaro 16 years, 6 months ago
Hi, I have a circle, I know its radius. I have a point on its edge. Is there anyway to figure out what angle in degrees that is?

           |
        ---|----
      /    |     \     
     P     |      \  
     |     |      |
 ----------O------X--------          
     |     |      |
      \    |     /
       \---|--- /
           |
           |
yeah that's a fine ascii circle, so anyway, I'm interested in the angle formed between P, O, X. I know the coordinates of all 3, I just want to know the angle between them. Thanks for any help p.s. - the formatting keeps messing up the circle (not that it was that great to begin with, hope it's understandable)
Advertisement
Assuming a programming language with an atan2() function the functions like the C math library function, atan2() should be able to give you the angle.
I'm using C++:

#include <math.h>
double atan2( double y, double x );

What am I feeding it though? I know the radius, and the two edge points,

Thanks
You're feeding it the coordinates of your point relative to the circle's origin, so you need to adjust the coordinates: X = O.x - P.x, Y = O.y - P.y. I don't know if they need to be inverted along the Y-axis, but this will become evident very quickly when you first test it.

GDNet+. It's only $5 a month. You know you want it.

Hi Tom,

My circle will always be at (0,0), so the edge points should always be relative to the center. I don't get how giving this atan2 function one set of (x,y) points will give you the proper angle - wouldn't it need to the second set of points to determine the angle formed between them?

Thanks
You know I really like these questions. Every few months I run into it again and realize I totally forgot how to solve it... After scratching my head for a bit here it is.

Let a = x-o
Let b = p-o
Let alpha be the angle you are trying to find

a cross b = ||a||*||b||*sin(alpha)
a dot b = ||a||*||b||*cos(alpha)

so

(a cross b) / (a dot b) = sin(alpha)/cos(alpha) = tan(alpha)

So alpha = atan( (a cross b) / (a dot b) )

Edit:

a cross b is a vector and you need a scalar. There is a 2d version of the cross product that gives a scalar... Oh my rusty brain...
Don't shoot! I'm with the science team.....
Quote:I don't get how giving this atan2 function one set of (x,y) points will give you the proper angle - wouldn't it need to the second set of points to determine the angle formed between them?
By 'second set of points', do you mean a 'second set of coordinates', i.e. a second x-y pair?

In any case, the angle returned by atan2() is always relative to the positive x axis (with positive angles being in the counter-clockwise direction), so only one input vector is required.
@jyk:
Yeah if atan2() gives the result relative to the positive x-axis that's perfect, exactly what I want. So if P = (-5,2) I would just do:

double Angle = atan2(2, -5);

and that's it?


@Oren:

I'm trying to remember by math from back in the day. I remember how to do cross products ok but the final formula confuses me:

alpha = atan( (a cross b) / (a dot b) )

I thought dot product returns a single value, while cross product leaves you with (in my case) 2 components - how do I feed that to atan after the (a cross b) / (a dot b) statement?


Sorry I'm rusty, I just want my airplane to rotate the right way!

Thanks
Quote:Original post by OrenGL
You know I really like these questions. Every few months I run into it again and realize I totally forgot how to solve it... After scratching my head for a bit here it is.

Let a = x-o
Let b = p-o
Let alpha be the angle you are trying to find

a cross b = ||a||*||b||*sin(alpha)
a dot b = ||a||*||b||*cos(alpha)

so

(a cross b) / (a dot b) = sin(alpha)/cos(alpha) = tan(alpha)

So alpha = atan( (a cross b) / (a dot b) )
I think you mean ||aXb|| in the above derivation, not aXb.

Also, it's best to use the 'atan2' function when you have it available, as it will handle gracefully the cases where the cosine is (near) zero. This then leaves you with:
angle = atan2(||aXb||,a.b)
Which is indeed an effective way to compute the angle between two 3-d vectors of arbitrary (but non-zero) length.

However, note that the returned angle will always be positive, so this can only be used to compute the unsigned angle between two vectors in 3-d.

Computing the signed angle between two 3-d vectors requires a frame of reference of some sort, but you'll note that the OP's problem is actually 2-d. Using similar logic to the derivation you posted above, we can compute the (signed) angle between two 2-d vectors of arbitrary (but non-zero) length as follows:
angle = atan2(perp(a).b,a.b)
Of course since the OP is asking for an absolute rather than relative angle, atan2(y,x) will do the trick, as previously noted.
Hmm if it's from the positive x-axis, then shouldn't:

    atan2(4.0, -0.3);


be greater than 90 degrees since it is already in quadrant II? I'm probably mis-using it -

Thanks

This topic is closed to new replies.

Advertisement