Created
February 22, 2018 09:22
-
-
Save james-roden/1164dea26b817ac5d5b3096621a7637b to your computer and use it in GitHub Desktop.
Python function to rotate a (x, y) coordinate by a specified angle
This file contains 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
# Rotation matrix function | |
def rotate_matrix (x, y, angle, x_shift=0, y_shift=0, units="DEGREES"): | |
""" | |
Rotates a point in the xy-plane counterclockwise through an angle about the origin | |
https://en.wikipedia.org/wiki/Rotation_matrix | |
:param x: x coordinate | |
:param y: y coordinate | |
:param x_shift: x-axis shift from origin (0, 0) | |
:param y_shift: y-axis shift from origin (0, 0) | |
:param angle: The rotation angle in degrees | |
:param units: DEGREES (default) or RADIANS | |
:return: Tuple of rotated x and y | |
""" | |
# Shift to origin (0,0) | |
x = x - x_shift | |
y = y - y_shift | |
# Convert degrees to radians | |
if units == "DEGREES": | |
angle = math.radians(angle) | |
# Rotation matrix multiplication to get rotated x & y | |
xr = (x * math.cos(angle)) - (y * math.sin(angle)) + x_shift | |
yr = (x * math.sin(angle)) + (y * math.cos(angle)) + y_shift | |
return xr, yr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment