Angle between two lines is wrong

后端 未结 3 1004
执笔经年
执笔经年 2021-02-06 07:11

I want to get angles between two line. So I used this code.


int posX = (ScreenWidth) >> 1;

int posY = (ScreenHeight) >> 1;

double radians, de         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-06 08:02

    Here's a function I use. It works great for me...

    float cartesianAngle(float x, float y) {
        float a = atanf(y / (x ? x : 0.0000001));
        if      (x > 0 && y > 0) a += 0;
        else if (x < 0 && y > 0) a += M_PI;
        else if (x < 0 && y < 0) a += M_PI;
        else if (x > 0 && y < 0) a += M_PI * 2;
        return a;
    }
    

    EDIT: After some research I found out you can just use atan2(y,x). Most compiler libraries have this function. You can ignore my function above.

提交回复
热议问题