HHD Site
 
 

 

 

Health and Human Development Home
  Dept of Kinesiology
 

HOW ARE TRANSFORMATIONS IMPLEMENTED IN JAVA?


Objective:    Show a 3D object in any specified configuration. 

Overview:


Source code:
// 3D MATRIX OBJECT DEFINITION //
class Matrix3D {
    float xx, xy, xz, xo;
    float yx, yy, yz, yo;
    float zx, zy, zz, zo;
    static final double pi = 3.14159265;

// TRANSLATE ORIGIN //
void translate(float x, float y, float z) {
 xo += x;
 yo += y;
 zo += z;
    }

// X-ROTATION //
void xrot(double theta) {
 theta *= (pi / 180);
 double ct = Math.cos(theta);
 double st = Math.sin(theta);

 float Nyx = (float) (yx * ct + zx * st);
 float Nyy = (float) (yy * ct + zy * st);
 float Nyz = (float) (yz * ct + zz * st);
 float Nyo = (float) (yo * ct + zo * st);

 float Nzx = (float) (zx * ct - yx * st);
 float Nzy = (float) (zy * ct - yy * st);
 float Nzz = (float) (zz * ct - yz * st);
 float Nzo = (float) (zo * ct - yo * st);
}

// MATRIX MULTIPLICATION //
void mult(Matrix3D rhs) {
 float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz;
 float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz;
 float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz;
 float lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + rhs.xo;

 float lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz;
 float lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz;
 float lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz;
 float lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + rhs.yo;

 float lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz;
 float lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz;
 float lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz;
 float lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + rhs.zo;
}

// TRANSFORM COORDINATES //
void transform(float v[], int tv[], int nvert) {
 float lxx = xx, lxy = xy, lxz = xz, lxo = xo;
 float lyx = yx, lyy = yy, lyz = yz, lyo = yo;
 float lzx = zx, lzy = zy, lzz = zz, lzo = zo;
 for (int i = nvert * 3; (i -= 3) >= 0;) {
     float x = v[i];
     float y = v[i + 1];
     float z = v[i + 2];
     tv[i    ] = (int) (x * lxx + y * lxy + z * lxz + lxo);
     tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo);
     tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo);
 }
}

// OUTPUT STRING //
public String toString() {
 return ("[" + xo + "," + xx + "," + xy + "," + xz + ";"
  + yo + "," + yx + "," + yy + "," + yz + ";"
  + zo + "," + zx + "," + zy + "," + zz + "]");
    }
}


Java Output:

Instructions:
Enter axis rotations (in degrees) and view object transformation. 


Return to Welcome Page