package torus;
/**
Simple 3d vector class with methods for simple operations.
Used in torus demo G. Hart Sept 2003.
*/
public class xyz {
public float x, y, z; // extract as .x .y and .z
xyz(float x0, float y0, float z0){ // constructor
x=x0; y=y0; z=z0;
}
public xyz(int x0, int y0, int z0){ // constructor from ints
x=(float)x0; y=(float)y0; z=(float)z0;
}
public String toString(){ // "(x,y,z)" string form
return "(" + Float.toString(this.x) + ","
+ Float.toString(this.y) + ","
+ Float.toString(this.z) + ")";
}
public static xyz plus(xyz a, xyz b){ // sum
return new xyz(a.x+b.x, a.y+b.y, a.z+b.z);
}
public static xyz minus(xyz a, xyz b){ // difference
return new xyz(a.x-b.x, a.y-b.y, a.z-b.z);
}
public static xyz scale(float a, xyz v){ // scalar prod
return new xyz(a*v.x, a*v.y, a*v.z);
}
public static float dot(xyz a, xyz b){ // dot prod
return a.x*b.x + a.y*b.y + a.z*b.z;
}
public static float mag2(xyz a){ // magnitude squared
return a.x*a.x + a.y*a.y + a.z*a.z;
}
public static float mag(xyz a){ // magnitude
return (float) Math.sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}
public static xyz unit(xyz v){ // unit length
return scale(1.0f/mag(v), v); // we hope v isn't 0
}
public static xyz cross(xyz a, xyz b){ // cross product
return new xyz(a.y*b.z - b.y*a.z, a.z*b.x - b.z*a.x, a.x*b.y - b.x*a.y);
}
}