How to get angle of point from center point?

混江龙づ霸主 提交于 2020-01-01 10:13:11

问题


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 dot?

Note, it should return an angle in degrees, from [0,360). The red dot angle in the image is approximately 70 degrees.

How can I do this in python?

Thanks

This doesn't seem to work.

        (dx, dy) = (x0-x, y-y0)
        angle = atan(float(dy)/float(dx))
        if angle < 0:
            angle += 180


回答1:


You were very close :-)

Change this:

 angle = atan(float(dy)/float(dx))

To this:

 angle = degrees(atan2(float(dy), float(dx)))

The atan2() function is between than atan() because it considers the signs to the inputs and goes all the way around the circle:

atan2(...)
    atan2(y, x)

    Return the arc tangent (measured in radians) of y/x.
    Unlike atan(y/x), the signs of both x and y are considered

The degrees() function converts from radians to degrees:

degrees(...)
    degrees(x)

    Convert angle x from radians to degrees.

Also, as Rich and Cody pointed-out you need to fix your dy calculation.




回答2:


In addition to converting from radians, consider using atan2 instead of atan. Whereas atan will give the same answer for points on the opposite side of the circle, atan2 will give you the correct angle, taking into account the signs of both dx and dy. It takes two arguments:

angle = math.degrees(math.atan2(y0 - y, x0 - x)) % 360

Note that atan2 will return something between -pi and pi, or -180 degrees and 180 degrees, so the % 360 is to shift the result to your desired range.




回答3:


Ah, easy mistake to make. atan returns the value in radians, not degrees. So you need to multiply the angle by 180/pi to get it back to degrees. You also need to change your dy to y0 - y to be consistent with your dx. Here's some corrected code.

dx, dy = x0-x, y0-y
angle_in_radians = atan2(dy,dx) # you don't need to cast to float
angle_in_degrees = angle_in_radians * 180 / pi



回答4:


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;
}


来源:https://stackoverflow.com/questions/22888773/how-to-get-angle-of-point-from-center-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!