Last active
May 27, 2022 02:10
-
-
Save somada141/d81a05f172bb2df26a2c to your computer and use it in GitHub Desktop.
Rotate a point coordinates around another point in Python #python #graphics #math
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#source: http://stackoverflow.com/questions/20023209/python-function-for-rotating-2d-objects | |
import math | |
def rotatePolygon(polygon,theta): | |
"""Rotates the given polygon which consists of corners represented as (x,y), | |
around the ORIGIN, clock-wise, theta degrees""" | |
theta = math.radians(theta) | |
rotatedPolygon = [] | |
for corner in polygon : | |
rotatedPolygon.append(( corner[0]*math.cos(theta)-corner[1]*math.sin(theta) , corner[0]*math.sin(theta)+corner[1]*math.cos(theta)) ) | |
return rotatedPolygon | |
def rotatePoint(centerPoint,point,angle): | |
"""Rotates a point around another centerPoint. Angle is in degrees. | |
Rotation is counter-clockwise""" | |
angle = math.radians(angle) | |
temp_point = point[0]-centerPoint[0] , point[1]-centerPoint[1] | |
temp_point = ( temp_point[0]*math.cos(angle)-temp_point[1]*math.sin(angle) , temp_point[0]*math.sin(angle)+temp_point[1]*math.cos(angle)) | |
temp_point = temp_point[0]+centerPoint[0] , temp_point[1]+centerPoint[1] | |
return temp_point |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, just pointing out that in most drawing frameworks (like pygame) Y-axis grows downwards so it actually rotates clockwise