How to get angle of point from center point?

前端 未结 4 955
傲寒
傲寒 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 17:12

    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.

提交回复
热议问题