public final class vector { // Class to manipulate a 3D vector
private double x, y, z;
public vector() { // Empty vector
x = 0;
y = 0;
z = 0;
}
public vector(double x, double y, double z) { // Create a vector
this.x = x;
this.y = y;
this.z = z;
}
public vector(vector v) { // Create a copy of a vector
x = v.x;
y = v.y;
z = v.z;
}
public vector(vector v1, vector v2) { // Create a vector by vector product
mult(v1, v2);
norm();
}
public double mult(vector v) { // Scalar product
return x * v.x + y * v.y + z * v.z;
}
private double norm2() { // Square of length
return x * x + y * y + z * z;
}
public double cosAng(vector v) { // Cosine of angle between vectors
return mult(v) / Math.sqrt(norm2() * v.norm2());
}
private void norm() { // Normalize vector
double d = Math.sqrt(norm2());
x /= d;
y /= d;
z /= d;
}
public void add(vector v) { // Add vector
x += v.x;
y += v.y;
z += v.z;
}
public void addmult(vector v, double d) { // Add scaled vector
x += v.x * d;
y += v.y * d;
z += v.z * d;
}
public void addmultnorm(vector v, double d) { // With normalization
addmult(v, d);
norm();
}
public void sub(vector v) { // Subtract vector
x -= v.x;
y -= v.y;
z -= v.z;
}
public void copy(vector v) { // Copy a vector
x = v.x;
y = v.y;
z = v.z;
}
private void mult(vector v1, vector v2) { // Vector product
x = v1.y * v2.z - v1.z * v2.y;
y = v1.z * v2.x - v1.x * v2.z;
z = v1.x * v2.y - v1.y * v2.x;
}
public void multnorm(vector v1, vector v2) { // With normalization
mult(v1, v2);
norm();
}
public void rotateX(double cos, double sin) { // Rotation about X axis
double y = this.y * cos + z * sin;
z = z * cos - this.y * sin;
this.y = y;
}
public void rotateY(double cos, double sin) { // Rotation about Y axis
double x = this.x * cos + z * sin;
z = z * cos - this.x * sin;
this.x = x;
}
public void rotateZ(double cos, double sin) { // Rotation about Z axis
double x = this.x * cos + y * sin;
y = y * cos - this.x * sin;
this.x = x;
}
private static String parameter(String param, char suffix, double d) {
if (d <= -1 || d >= 1 || d == 0) return "";
else if (d < 0) return "";
else return "";
}
public void writeParameters(String param) {
System.err.println(parameter(param, 'x', x));
System.err.println(parameter(param, 'y', y));
System.err.println(parameter(param, 'z', z));
}
}