Rotate around a specific point instead of 0,0,0

4.7k Views Asked by At

I have this function to map a 3d point to a 2d face. Right now, any of the rotations will rotate around 0,0,0. How would I do it so I can set the point they rotate around?

def project(point,rx=0,ry=0,rz=0):
    x,y,z = point
    x0=x
    y0=y*math.cos(rx)+z*math.sin(rx)
    z0=z*math.cos(rx)-y*math.sin(rx)
    x1=x0*math.cos(ry)-z0*math.sin(ry)
    y1=y0
    z1=z0*math.cos(ry)+x0*math.sin(ry)
    x2=x1*math.cos(rz)+y1*math.sin(rz)
    y2=y1*math.cos(rz)-x1*math.sin(rz)
    return (int(x2),int(y2))
1

There are 1 best solutions below

13
On BEST ANSWER

To rotate around a specific point the standard trick is make at first a translation taking P in O, then the rotation around the new origin O and finally the backward translation.

In matrix notation instead of $u’=Ru$ you need to calculate $u’=R(u-OP)+OP$, where

  • u is the vector to rotate

  • u’ is the rotated vector

  • R is the rotation matrix

  • OP is the vector from O to P (that is P-O)