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

Obtain a Degree from Position

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]

Comments

  • XLoomXLoom 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;
    // the angle in radians
    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;

    // or degrees into radians
    angle=(degrees/180)*PI;

    I hope this helps. (And I also hope here are no accidental mistakes, because I have been doing really ugly math in university for all day, and my brain is somewhat exhausted :)

    XLoom

Sign In or Register to comment.