Obtain a Degree from Position - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Obtain a Degree from Position

Posts: 84Member
Im building a Graphic 2D library now(I want to upload it but I dont now where). Do you know how to get degree from position?

[code]
Example:
x1=100
y1=100
x2=300
y2=400

The angle are?
[/code]

• Posts: 129Member
Hi,

The way to get the angle from position is very simple.

// the first position
float x1,y1;
// the target position
float x2,y2;
// vector from positions
float vx,vy;
float angle;

vx=x2-x1;
vy=y2-y1;

// This will give you right angle in radians.
// Normally you would have to check whether the vx is not 0, but
// double atan2(double y,float x) function
// (dont forget to #include )
// can manage it and it will also give you proper -PI to PI value
// unlike atan
angle=atan2(vy,vx);

// if you want to convert the radians into degrees you can do this
#define PI 3.14159265358979323846

float degrees;

degrees=(angle/PI)*180;