package myGraphics.VectorGrafix; import java.lang.Math; import myGraphics.VectorGrafix.Vector; public class Vector3d extends Vector { public static Vector3d ORIGIN = new Vector3d(0.0, 0.0, 0.0); public Vector3d() { vterm = new double[4]; vterm[0] = 0.0; vterm[1] = 0.0; vterm[2] = 0.0; vterm[3] = 1.0; } public Vector3d(double the_x, double the_y, double the_z) { vterm = new double[4]; vterm[0] = the_x; vterm[1] = the_y; vterm[2] = the_z; vterm[3] = 1.0; } public Vector3d(double the_x, double the_y, double the_z, double the_w) { vterm = new double[4]; vterm[0] = the_x; vterm[1] = the_y; vterm[2] = the_z; vterm[3] = the_w; } public Vector3d copy() { return new Vector3d(x(), y(), z(), w()); } public Vector3d negate() { /* NOTE: the w term is not negated, it should remain 0 or 1 */ return new Vector3d(-x(), -y(), -z(), w()); } public double x() { return vterm[0]; } public double y() { return vterm[1]; } public double z() { return vterm[2]; } public double w() { return vterm[3]; } public double set_x(double value) { return set_term(0, value); } public double set_y(double value) { return set_term(1, value); } public double set_z(double value) { return set_term(2, value); } public double set_w(double value) { return set_term(3, value); } public double dot_product(Vector3d other) { /* * NOTE: To use this for vectors in 3-space, the w * terms should be set to 0.0 before this method * is called. */ double sum = 0; for (int i = 0; i < 3; i++) { sum += vterm[i] * other.vterm[i]; } return sum; } public Vector3d cross_product(Vector3d other) { Vector3d result = new Vector3d(); result.set_x(y() * other.z() - z() * other.y()); result.set_y(z() * other.x() - x() * other.z()); result.set_z(x() * other.y() - y() * other.x()); result.set_w(0.0); return result; } public Vector3d projection_onto(Vector3d other) { return (other.times(this.dot_product(other) / other.dot_product(other))); } public Vector3d plus(Vector3d other) { return new Vector3d(x() + other.x(), y() + other.y(), z() + other.z(), 0.0); } public Vector3d minus(Vector3d other) { return new Vector3d(x() - other.x(), y() - other.y(), z() - other.z(), 0.0); } public Vector3d times(double scalar) { return new Vector3d(x() * scalar, y() * scalar, z() * scalar, 0.0); } public Vector3d divided_by(double scalar) { return new Vector3d(x()/scalar, y()/scalar, z()/scalar, 0.0); } public boolean equals(Vector3d other) { for (int i = 0; i <= 3; i++) { if (vterm[i] != other.get_term(i)) { return false; } } return true; } public double magnitude() { return Math.sqrt(this.dot_product(this)); } public Vector3d normalize() { return this.divided_by(this.magnitude()); } public double angleBetween(Vector3d other) { return Math.acos(this.dot_product(other) / this.magnitude() * other.magnitude()); } public double distanceTo(Vector3d other) { Vector3d temp = new Vector3d(); temp = other.minus(this); return temp.magnitude(); } }