Mike G. answered 11/12/23
Seasoned engineer with 20+ years experience in software
To rotate a point around another point (cx, cy) by an angle, the correct mathematics involve a rotation matrix. The formulas for rotating a point (px, py) around the origin and then translating it back are as follows:
Comparing this to your provided code:
The part of your code given here:
is the correct way to apply the rotation part of the transformation. This part effectively rotates the point around the origin (0,0). Since you first translate your point p to the origin with p.x -= cx; p.y -= cy;, this rotation is effectively around the point (cx, cy). After applying this rotation, you would need to translate the point back to its original location. You can do this by adding cx to xnew and cy to ynew
So, your first approach is the correct one for rotating a point around another point. Remember to translate the point back after rotating it.