Followers 0

# Angle between two 2D points

## 37 posts in this topic

I'm trying to find the angle between two points in 2D, A and B. A is the location of a sprite, and B is the location of the mouse. I want to set the rotation of the sprite to the angle between the two points, so that the sprite appears to follow the mouse. I've been told that the dot product of two unit vectors returns the cosine of the angle between them, in the range -1 to 1, so the angle between the vectors must be... theta = acos(A.x * B.x + A.x * B.y) theta of course, is represented in radians. Now, the library I'm using is SFML, which requires that the angle for the sprite be passed to it in degrees, so it's as simple as Sprite::SetRotation(theta * 180.0f / PI), but the problem is, acos() seems to only be returning a value in the range 0 to PI/2 radians aka 0 to 90 degrees. How do I get an angle in the range 0 - 360 instead?
0

##### Share on other sites
float CMath::Angle( const CPoint* a, const CPoint* b){	return ToDegrees( atan2( b->y - a->y, b->x - a->x ) );}float CMath::Angle( const CPoint* b){	CPoint a(0,0,0);	return Angle( &a, b );}float CMath::ToDegrees( float radians ){	return radians * DEGREES_PER_RADIAN;}float CMath::ToRadians( float degrees ){	return degrees * RADIANS_PER_DEGREE;}
0

##### Share on other sites
haha, I suppose that will do it :)

I was just curious as to how to do it using the dot product. I'm still interested to hear if someone knows the answer.

0

##### Share on other sites
How exactly do you define "angle between points"?

Points are dimensionless entities with no direction in any n-space. The only sane measure between two points that I am aware of (at 10 in the morning) is distance.

Possibly you mean angle between vectors?
0

##### Share on other sites
phresnel, I'm guessing that you mean that there's not such thing as angle between points on a 1D line... I would have thought specifying points in 2D space would have made it obvious that I meant 2D vectors :)
0

##### Share on other sites
It's off of the top of my head, but I'm believe it is correct. Test it though to make sure :)

As for using acos, check here:
http://www.cppreference.com/wiki/c/math/acos

You can use it but you need one more logic step afterwards to determine if your result should be positive or negative (which you can then calc out of 360). I find the atan2 solution to be cleaner, personally.
0

##### Share on other sites
Quote:
 Original post by phresnelHow exactly do you define "angle between points"?Points are dimensionless entities with no direction in any n-space. The only sane measure between two points that I am aware of (at 10 in the morning) is distance.Possibly you mean angle between vectors?

Often (and incorrectly so), points and vectors are used interchangeably in game dev, especially 2D game dev. Basically considering a point to be a vector originating from the origin. I've seen this from indies, hobbyists, and commercial shops :)

This becomes a nuisance eventually, though, with "w" values. For the most part, 2D games can save on some code by having a joint "point"/"vector" class, even though it is amathematically incorrect model :D

So in the OP's case, it's really mapping one origin vector to another and determine the angle between them.
0

##### Share on other sites
Quote:
Quote:
 Original post by phresnelHow exactly do you define "angle between points"?Points are dimensionless entities with no direction in any n-space. The only sane measure between two points that I am aware of (at 10 in the morning) is distance.Possibly you mean angle between vectors?

Often (and incorrectly so), points and vectors are used interchangeably in game dev, especially 2D game dev. Basically considering a point to be a vector originating from the origin. I've seen this from indies, hobbyists, and commercial shops :)

This becomes a nuisance eventually, though, with "w" values. For the most part, 2D games can save on some code by having a joint "point"/"vector" class, even though it is amathematically incorrect model :D

So in the OP's case, it's really mapping one origin vector to another and determine the angle between them.

I think it is only beneficial to seperate those models (and comes at no extra runtime cost), as I made the experience that it makes code clearer (i.e. intentions) and less error prone (operations that don't make sense are forbidden). Some operations are also distinct, dependent on whether the underlying concept is point, vector, or normal.

As you see, it already confused me to see some angle between points, which only makes sense if we assume some coordinate origin to both.

To give an example of how I am used to it:
   Point A, B;   ...   Vector direction = B-A;   float length = direction.length();   Point newP = A + direction * 3;

Point+Point is not defined, Point-Point yields a Vector, Point[+-]Vector yields a new Point, et cetera.

In picogen, the distinction is important for yet another reason: Points and Vectors are allowed to use different scalar types. I need this so that I can use fixed-point arithmetic for points, but floating point math for directions. Thanks to that distinction and the thorough constraints upon Points (operator+, operator-, distance), only the most basic fixed point math is needed (addition, subtraction, comparison).

...

Probably I am just too drilled by writing ray tracers, where the mathematical model is often common ;)
0

##### Share on other sites
Quote:
 Original post by phresnelI think it is only beneficial to seperate those models (and comes at no extra runtime cost), as I made the experience that it makes code clearer (i.e. intentions) and less error prone (operations that don't make sense are forbidden). Some operations are also distinct, dependent on whether the underlying concept is point, vector, or normal.As you see, it already confused me to see some angle between points, which only makes sense if we assume some coordinate origin to both. To give an example of how I am used to it: Point A, B; ... Vector direction = B-A; float length = direction.length(); Point newP = A + direction * 3;Point+Point is not defined, Point-Point yields a Vector, Point[+-]Vector yields a new Point, et cetera. In picogen, the distinction is important for yet another reason: Points and Vectors are allowed to use different scalar types. I need this so that I can use fixed-point arithmetic for points, but floating point math for directions. Thanks to that distinction and the thorough constraints upon Points (operator+, operator-, distance), only the most basic fixed point math is needed (addition, subtraction, comparison)....Probably I am just too drilled by writing ray tracers, where the mathematical model is often common ;)

No doubt, I agree absolutely. I've worked with systems using both approaches and found it's only sane to seperate them in large scale, math heavy projects. Of course, if I feel like writing a 2D shooter in a weekend, it's fun to pretend to add two points together :D
0

##### Share on other sites
Sorry, but I dont understand the distinction between a vector and a point. Isn't a point just a vector from <0, 0> ?
0

##### Share on other sites
Quote:
 Sorry, but I dont understand the distinction between a vector and a point. Isn't a point just a vector from <0, 0> ?
I'm not going to try to answer the question from a rigorous mathematical perspective, but I will say that yes, in practice, points are often considered as displacements from the origin in the context of games and graphics programming.
Quote:
 I would have thought specifying points in 2D space would have made it obvious that I meant 2D vectors :)
Not really. Whether you're using separate point and vectors classes or using a vector class to represent both vectors and points, it's still important to use correct terminology when talking about geometrical problems.

The phrase 'angle between two points' isn't self-explanatory (IMO, at least). I can think of at least two possible interpretations:

1. It's the angle between the vectors from the origin to each point that you're interested in.

2. It's the absolute angle of the vector from one point to the other that you're interested in.

The rest of your post makes it pretty clear that it's number 2 that you're after, in which case the 'atan2' solution suggested by shadowcomplex is probably the most straightforward solution.

The 'acos' method can be used to compute the unsigned relative angle between two vectors. With a little extra work it can be used to compute the signed angle as well, in which case you could solve the stated problem by finding the relative angle between the vector from A to B and the x axis.

However, it's more straightforward to use atan2 if you have it available. (Personally, I prefer to use atan2 for all 'angle between vectors' problems, as it eliminates the need for the input vectors to be unit-length, and sidesteps some numerical issues that have to be dealt with when using the 'acos' method.)
0

##### Share on other sites
Quote:
 Original post by _Sauce_Sorry, but I dont understand the distinction between a vector and a point. Isn't a point just a vector from <0, 0> ?

A point is a location

A vector is a direction and a magnitude

It makes sense to talk about the angle between two directions, but there is no such thing as an angle between two locations. What's the angle between New York and Sydney?
0

##### Share on other sites
Well, your high school math teacher really didn't do his job very well.

Points and vectors are two different mathematics concepts. For instance, addition of two vectors is well defined and is having an obvious geometric analogy behind. So, do you know what is the meaning of adding two points together? Frankly, I have no idea. Maybe, you can define a custom meaning on your own. However, it is redundant for you to do that.

Even if points and vectors behave in the same way in your case, you still want to use the technical terms in their conventional way for more effective communication. Just in case you forgot, the primary objective of writing is communication.
0

##### Share on other sites
Quote:
 Original post by MelekorIt makes sense to talk about the angle between two directions, but there is no such thing as an angle between two locations. What's the angle between New York and Sydney?

0

##### Share on other sites
What are the GPS co-ords of new york and sydney, or should it actually be called GVS?
0

##### Share on other sites
Quote:
 Original post by Interesting DaveWhat are the GPS co-ords of new york and sydney, or should it actually be called GVS?

Elaborate.
0

##### Share on other sites
GPS would give a point = position = location, right?

the GPS co-ordinates for new york and sydney can be used with atan2 to get the angle between them (angle between two points).

Or are we saying that GPS should actually be called Global Vectoring System (/Satelites)?
0

##### Share on other sites
Quote:
 Original post by Interesting DaveGPS would give a point = position = location, right?the GPS co-ordinates for new york and sydney can be used with atan2 to get the angle between them (angle between two points).Or are we saying that GPS should actually be called Global Vectoring System (/Satelites)?

GPS would give a lattitude / longitude per city. You'd get 4 values. Atan2 takes two arguments. See a flaw ?
0

##### Share on other sites
Thanks jyk, it becomes very clear when you explain it like that :)

ma_hty, I don't know about Hong Kong, but where I went to school vector math wasn't part of the curriculum. That said, I never studied Math C, which probably covered it.

Now, obviously calculating the length of the vector between the two vectors (in order to normalise them) is pretty slow due to the sqrt(), so the atan2() method is likely faster, but would it be faster to calculate the angle using the dot product if the two vectors were already unit-length?

Just as an off-the-side here, I'm not certain I'm understanding the dot product method 100% - do both of the input vectors have to be normalised in order for it to work, or just the vector that lies between them?
0

##### Share on other sites
Quote:
 Original post by Interesting DaveOr are we saying that GPS should actually be called Global Vectoring System (/Satelites)?

There are devices that only give you the direction and magnitude to a target entity (a better compass), so I think it's not the perfect example.
0

##### Share on other sites
Quote:
 Original post by _Sauce_Just as an off-the-side here, I'm not certain I'm understanding the dot product method 100% - do both of the input vectors have to be normalised in order for it to work, or just the vector that lies between them?

Neither need the input vectors to have unit length, nor does the vector in-between need so. The correspondence is
a . b = |a| * |b| * cos( <a,b> )
what is to be read like "the dot product of vectors a and b is the same as the product of their lengthes and the cosine of the angle in-between them".

Furthur, the usual method to compute the dot-product is
a . b = sumi( ai * bi )
and is effort-wise totally independent of the length of the both vectors. Only if you want to use the above correspondence you can win with the vectors having unit length, because in that special case
a . b = 1 * 1 * cos( <a,b> ) == cos( <a,b> )

Quote:
 Original post by _Sauce_but the problem is, acos() seems to only be returning a value in the range 0 to PI/2 radians aka 0 to 90 degrees. How do I get an angle in the range 0 - 360 instead?

The acos should return values in the range of 180 degrees, not 90 degrees. If you think of the above formulation "the angle in-between them" then you can see that there is no order mentioned, i.e. neither "the angle from a to b" nor "the angle from b to a". Without an order, the angle can only be in [0,180] degrees. This is due to the periodicity of the cosine, so that its inverse function (acos) must be restricted.

(Hopefully that wasn't already written and overseen by me ;))
0

##### Share on other sites
Quote:
 Original post by YsaneyaGPS would give a lattitude / longitude per city. You'd get 4 values. Atan2 takes two arguments. See a flaw ?

I guess what I mean is that the original question "angle between two points" is valid. Technically it should be "angle of vector between two points" as jyk pointed out, but since the answer can be derieved from just the two points, its doable - and possibly contrary to what Melekor said:

Quote:
 Original post by Melekorthere is no such thing as an angle between two locations. What's the angle between New York and Sydney?

x = sydney.longitude - newyork.longitude
y = sydney.latitude - newyork.latitude
angle = atan2(y, x)

Assuming that 2 co-ordinates on the same 'grid' are classed as points, and not vectors from the origin of said 'grid'

[added:] I realise that would be the long way around, since it wouldn't take into fact the world is planet shaped...
0

##### Share on other sites
Quote:
 Original post by Interesting Dave... "angle of vector between two points"

A single vector (it does not matter where it came from) has no angle!
So you mean to say, that the angle of a vector between two points and the ?-axis, where ? could be x or y, dependent on the convention used.

Sorry to be a pain in the ass but this thread is kind of funny.
0

##### Share on other sites
Quote:
 Original post by Interesting Davex = sydney.longitude - newyork.longitudey = sydney.latitude - newyork.latitudeangle = atan2(y, x)Assuming that 2 co-ordinates on the same 'grid' are classed as points, and not vectors from the origin of said 'grid'

Hi,
I don't agree with that. In common language (like in, ask someone in the street), there's no such a thing as an angle between two points.

Now, if we really have to make that mean something, the safest is to assume a point is the same thing as a vector, that is an element of R^2 and as such, the angle would be arctan(y2/x2) - arctan(y1/x1)

(which is not what the OP is after)

0

##### Share on other sites
Quote:
Original post by dragongame
Quote:
 Original post by Interesting Dave... "angle of vector between two points"

A single vector (it does not matter where it came from) has no angle!
So you mean to say, that the angle of a vector between two points and the ?-axis, where ? could be x or y, dependent on the convention used.

Sorry to be a pain in the ass but this thread is kind of funny.

Yes thats a very good point. I was just going to assume that 'north' would be the base axis :P
0

## Create an account

Register a new account