How to get the point angle from the center point?

If I have 2 points (x0, y0) that are the center of the circle and the other point (x, y) (this is the red dot on the border of the circle in the image). How can I get the point angle?

Note that it must return the angle in degrees, from [0.360). The angle of the red dot in the image is about 70 degrees.

How can i do this in python?

thank

This does not work.

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

enter image description here

+4
source share
4 answers

You were very close :-)

Change this:

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

For this:

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

The atan2 () function is between what atan () is because it considers the signs on the inputs and goes 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 is converted from radians to degrees:

degrees(...)
    degrees(x)

    Convert angle x from radians to degrees.

, Rich Cody, dy.

+4

, atan2 atan. atan , atan2 , dx, dy. :

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

, atan2 - -pi pi, -180 180 , % 360 .

+3

, . atan , . 180/pi, . dy y0 - y, dx. .

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

Source: https://habr.com/ru/post/1535198/


All Articles