For a context that I won’t fall into, I need two functions that are essentially mutual from each other.
angle_to() must return the number of degrees clockwise to go from 0 ° to the line connecting p1 to p2 (i.e. p1 is the center of rotation), and where both p1 and p2 are pixel coordinates.
point_pos() should return the pixel coordinates where the hour of the amplitude should change angle .
For both sides, the positive axis is x = 0 ° = 3 hours, and the rotation argument must shift this axis before the calculation starts either clockwise or counterclockwise; then the specified calculation should move in the same direction with this adjusted link.
My progress on each of them is given below; failure:
When clockwise = False, it returns the correct answer for the state clockwise; when clockwise = True, angle_between () returns the correct answer with a rounding error, and point_pos () gives me the wrong answer entirely.
I also added a visual explanation, which I mocked in Illustrator as an apology on the Internet for not being able to solve it, and it is unclear if I'm looking.
Edit: clearing a line that is unnecessarily complicated according to one answer below.
from math import sin, cos, radians, pi, atan2, degrees def angle_to(p1, p2, rotation=0, clockwise=False): if abs(rotation) > 360: rotation %= 360 p2 = list(p2) p2[0] = p2[0] - p1[0] p2[1] = p2[1] - p1[1] angle = degrees(atan2(p2[1], p2[0])) if clockwise: angle -= rotation return angle if angle > 0 else angle + 360 else: angle = (360 - angle if angle > 0 else -1 * angle) - rotation return angle if angle > 0 else angle + 360 def point_pos(origin, amplitude, angle, rotation=0, clockwise=False): if abs(rotation) > 360: rotation %= 360 if clockwise: rotation *= -1 if clockwise: angle -= rotation angle = angle if angle > 0 else angle + 360 else: angle = (360 - angle if angle > 0 else -1 * angle) - rotation angle = angle if angle > 0 else angle + 360 theta_rad = radians(angle) return int(origin[0] + amplitude * cos(theta_rad)), int(origin[1] + amplitude * sin(theta_rad))

Edit # 2:. Upon request, an unsuccessful conclusion appeared:
angle_to() flips clockwise and counterclockwise (when I tried to fix it, I end up getting the wrong answers) and rotates clockwise and computes in different directions
>>> print angle_to((100,100), (25,25))
point_pos() simply wrong in the counterclockwise direction