• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Angle from two points

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

## Recommended Posts

I should know this... but anyway... What''s a fast way to calculate the angle between 2 points in the Cartesian coordinates system. -Derek

##### Share on other sites
You mean the angle between the two vectors from the origin to the two points? Take the arccos of the dot product of the two vectors, divided by the magnitude of each vector.

cos theta = a . b / (/a/ * /b/).

##### Share on other sites
I forgot that the board mangles pipes. Those last two /a/ and /b/ respectively refer to magnitude of a and magnitude of b.

##### Share on other sites
Here''s some code if you want...

#include #include #define PI 3.1415926535897932385class CVector2d{	private:		float x, y;	public:		CVector2d() : x(0.0f), y(0.0f) {}		CVector2d(float vx, float vy) : x(vx), y(vy) {}		float Magnitude();		float Dot(CVector2d &a);};float CVector2d::Magnitude(){	return (float)sqrt(pow(x,2) + pow(y,2));}float CVector2d::Dot(CVector2d &a) {	return (a.x * x) + (a.y * y);}void main(void){	CVector2d a(1,0), b(0,1);	float cosTheta, degrees, radians = 0.0f;	cosTheta = a.Dot(b) / (a.Magnitude() * b.Magnitude()); 	radians = (float)(acos(cosTheta));	degrees = (float)(radians * (180/PI));	cout << "radians: " << radians << endl;	cout << "degrees: " << degrees << endl;}

##### Share on other sites
That was my post above, but it looks like it removed the includes....it''s including math.h (for acos()) and iostream.h (for cout()).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015228
×