Reputation:
I have small question, I have functions for rotating lines in 2D, and it works only when C[0]==C1, where should be a mistake? if C[0]!=C1 rotated line is longer than original.
def rotmat(alpha): # angle in radians
ca, sa = cos(alpha), sin(alpha)
return array([[ca, -sa],[sa, ca]])
def rotate(X, Y, alpha, C=(0,0)):
"""
X,Y - coordinates of points, C - center of rotation, default (0,0)
"""
Ca = asarray(C)
XY = asarray([X,Y])
Xr,Yr = Ca + dot(rotmat(alpha), XY-Ca)
return Xr,Yr
X = (1,2); Y=(2,1)
Xr,Yr = rotate(X,Y,pi/2.,(0.,5.))
plot(X,Y,Xr,Yr,'r')
axis('scaled')
show()
blue is original, and red is rotated
Upvotes: 0
Views: 1330
Reputation:
Vectors do not have a location in point space. If D
is the rotation matrix, they simply transform as v' = Dv
, not as v' = D(v-c)+c
like your code does.
You are handling the vector as if it was representing the coordinates of a point and presumably you are then measuring the distance to the origin in these coordinates.
If the rotation center is not the origin, then this distance is not invariant under the rotation, while the length of a vector is always invariant under rotation.
Edit after edit of question:
Your subtraction of Ca
is wrong. Numpy broadcasting will result in the center being subtracted from each row instead of each column as you intended. You can fix this for example by constructing a full matrix for Ca
:
Ca = asarray([[C[0]]*len(X), [C[1]]*len(X)])
There probably is some nicer numpy solution to do the correct shape casting that I am unaware of right now.
Upvotes: 2