How to get angle of point from center point?

前端 未结 4 956
傲寒
傲寒 2021-02-09 16:34

If I have 2 points (x0,y0) which is the center of the circle, and another point (x,y) (this is the red dot on the circle boundary in the image). How can I get the angle of the d

4条回答
  •  故里飘歌
    2021-02-09 16:57

    float AnglePointToPoint(const CCPoint & pFrom, const CCPoint & pTo)
    {
        float distanceX     = pTo.x - pFrom.x;
        float distanceY     = pTo.y - pFrom.y;
        float beta          = acos( fabs(distanceX) / sqrt( pow(distanceX,2) + pow(distanceY,2) ) ) * 180 / M_PI;
        float angleResult   = 0.0f;
    
        if( distanceX > 0 )
        {
            if( distanceY < 0 )
            {
                angleResult = beta + 90;//right_bot
            }
            else
            {
                angleResult = fabs(beta - 90);//right_top
            }
        }
        else
        {
            if( distanceY < 0 )
            {
                angleResult = fabs(beta - 90) + 180;//left_bot
            }
            else
            {
                angleResult = beta + 270;//left_top
            }
        }
        return angleResult;
    }
    

提交回复
热议问题