python - rotating collection of spheres with respect to the z-axis -
i have coordinates (x,y,z) of center of spheres stored in numpy array. want able rotate spheres respect z-axis getting weird results. code rotation seems moving , right. maybe intended result rotation, don't think is. here code:
theta = math.pi/6 ct = math.cos(theta) st = math.sin(theta) z = np.array([[ct, -st, 0], [st, ct, 0], [0, 0, 1]]) self.atoms = np.array([[90,100, 1], [140,100, 1]]) self.atoms = self.atoms.dot(z)
here image looked before rotation:
and here looks after:
you have translate entire system center of rotation center of system.
the equations used rotation use work if rotating around origin.
for translation can multiply matrix having direction of translation last row.
anyway, entire transformation p' = inv(t) * r * t * p
(where p
each point of figure , p'
in final result, see example) inverse of translation matrix negate sign of translation components.
edit (worked out example -- might have transpose -- switch rows columns):
you start points placed at:
atoms = 90 140 100 100 1 1
which presented in
then apply rotation matrix
r = 0.86603 -0.50000 0.00000 0.50000 0.86603 0.00000 0.00000 0.00000 1.00000
and result of
r * atoms = 27.9423 71.2436 131.6025 156.6025 1.0000 1.0000
which translates (as observed, red points new ones)
the problem doing r * atoms
rotate around origin. in following figure, angle between 2 blue lines pi/6
now, we'd obtain blue circles in:
to need several steps:
build translation matrix convert points such center of rotation center of axes:
t = 1 0 -115 0 1 -100 0 0 1
(the
-115
,-100
negatives of center of atoms)translate points such 2 centers overlap (and red atoms)
t * atoms = -25 25 0 0 1 1
(observe our new 2 points symmetrical around origin)
rotate new points (and green circles)
r * t * atoms -21.6506 21.6506 -12.5000 12.5000 1.0000 1.0000
finally, translate back
inv(t) * r * t * atoms = 93.3494 136.6506 87.5000 112.5000 1.0000 1.0000
final remarks:
the output you've obtained explicable due fact origin in lower corner/middle of figure while yours in upper corner.
because of might have reverse order of multiplications:
point * translation * rotation * translation
. check works properly.i cheated little , did transformation on both points @ same time. luckily, results same if doing transformation on each point in turn.
every 2d/3d transform can written in term of matrices. use
3x3
matrices 2d ,4x4
3d this. basically, work homogenous coordinatesfinally, see transformation matrix more examples , details.
Comments
Post a Comment