**0**

# Angle between two 2D points

###
#1
Members - Reputation: **116**

Posted 23 August 2009 - 08:43 PM

###
#2
Members - Reputation: **445**

Posted 23 August 2009 - 08:49 PM

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;

}

###
#6
Members - Reputation: **445**

Posted 23 August 2009 - 09:02 PM

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.

###
#7
Members - Reputation: **445**

Posted 23 August 2009 - 09:16 PM

Quote:

Original post by phresnel

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?

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.

###
#8
Members - Reputation: **949**

Posted 23 August 2009 - 10:16 PM

Quote:

Original post by shadowcomplexQuote:

Original post by phresnel

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?

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 ;)

###
#9
Members - Reputation: **445**

Posted 23 August 2009 - 11:07 PM

Quote:

Original post by phresnel

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 themathematicalmodel 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

###
#11
Members - Reputation: **2078**

Posted 24 August 2009 - 01:06 AM

Quote: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.

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

Quote: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.

I would have thought specifying points in 2Dspace would have made it obvious that I meant 2D vectors :)

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.)

###
#12
Members - Reputation: **379**

Posted 24 August 2009 - 01:08 AM

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?

###
#13
Members - Reputation: **100**

Posted 24 August 2009 - 01:20 AM

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.

###
#17
Members - Reputation: **100**

Posted 24 August 2009 - 02:01 AM

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)?

###
#18
Members - Reputation: **1314**

Posted 24 August 2009 - 02:10 AM

Quote:

Original post by Interesting Dave

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)?

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

###
#19
Members - Reputation: **116**

Posted 24 August 2009 - 02:12 AM

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?

###
#20
Members - Reputation: **949**

Posted 24 August 2009 - 02:24 AM

Quote:

Original post by Interesting Dave

Or 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.