Skip to content

Instantly share code, notes, and snippets.

@somada141
Last active May 27, 2022 02:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somada141/d81a05f172bb2df26a2c to your computer and use it in GitHub Desktop.
Save somada141/d81a05f172bb2df26a2c to your computer and use it in GitHub Desktop.
Rotate a point coordinates around another point in Python #python #graphics #math
#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
@nagy135
Copy link

nagy135 commented Jul 28, 2021

Thanks for this, just pointing out that in most drawing frameworks (like pygame) Y-axis grows downwards so it actually rotates clockwise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment