Search code examples
mit-scratch

Four dimensional script in a efficient manner using variables Xrotation, Yrotation, and Zrotation


Is there an efficient formula to map a four dimensional point in a three dimensional space using the variables Xrotation Z rotation and Yrotation as inputs?

I specifically need scratch language, but any other language will be suitable.


Solution

  • The four dimensional rotation you're probably talking about is called a quaternion. When talking about rotating around each axis in a 3D space, we call Euler angles. Wikipedia has page about these conversions, which includes this python source:

    class Quaternion_toEulerianAngle():
        def __init__(self, x, y, z, w):
            self.x = x
            self.y = y
            self.z = z
            self.w = w
    
        def X(self):
            ysqr = self.y*self.y
    
            t0 = +2.0 * (self.w * self.x + self.y*self.z)
            t1 = +1.0 - 2.0 * (self.x*self.x + ysqr)
            X = math.degrees(math.atan2(t0, t1))
    
            return X
    
        def Y(self):
            ysqr = self.y*self.y
    
            t2 = +2.0 * (self.w*self.y - self.z*self.x)
            t2 =  1 if t2 > 1 else t2
            t2 = -1 if t2 < -1 else t2
            Y = math.degrees(math.asin(t2))
    
            return Y
    
        def Z(self):
            ysqr = self.y*self.y
    
            t3 = +2.0 * (self.w * self.z + self.x*self.y)
            t4 = +1.0 - 2.0 * (ysqr + self.z*self.z)
            Z = math.degrees(math.atan2(t3, t4))
    
            return Z