Jump to content
• Advertisement

# angle in a circle

This topic is 3928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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)

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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...

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
@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

#### Share this post

##### Share on other sites
Quote:
 Original post by OrenGLYou 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-oLet b = p-oLet alpha be the angle you are trying to finda 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.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5
• Advertisement

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631715
• Total Posts
3001868
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!