Jump to content
  • Advertisement

Archived

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

DerekDay

Angle from two points

This topic is 6791 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Here''s some code if you want...


#include
#include

#define PI 3.1415926535897932385

class 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 this post


Link to post
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()).

Share this post


Link to post
Share on other sites

  • Advertisement
×

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!