Last active
February 11, 2016 08:38
-
-
Save sungyongchoi/e884e5c86277af4752c2 to your computer and use it in GitHub Desktop.
rotate points
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
import math | |
def rotate_vector(v, angle, anchor): | |
"""Rotate a vector `v` by the given angle, relative to the anchor point.""" | |
x, y = v | |
x = x - anchor[0] | |
y = y - anchor[1] | |
# Here is a compiler optimization; inplace operators are slower than | |
# non-inplace operators like above. This function gets used a lot, so | |
# performance is critical. | |
cos_theta = math.cos(math.radians(angle)) | |
sin_theta = math.sin(math.radians(angle)) | |
nx = x*cos_theta - y*sin_theta | |
ny = x*sin_theta + y*cos_theta | |
nx = nx + anchor[0] | |
ny = ny + anchor[1] | |
return [nx, ny] | |
print rotate_vector((1,0),45,(0,0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment