/** 3D vector math * (Note that java.util.Vector is a predefined Java class) * @author: Phil Gage */ class Vector3 { public double x, y, z; Vector3 () { this.x = 0.0; this.y = 0.0; this.z = 0.0; } Vector3 (Vector3 v) { this.x = v.x; this.y = v.y; this.z = v.z; } Vector3 (Point p) { this.x = p.x; this.y = p.y; this.z = p.z; } Vector3 (double x, double y, double z) { this.x = x; this.y = y; this.z = z; } /** Copy fields from another vector */ public void set (Vector3 a) { x = a.x; y = a.y; z = a.z; } /** Copy fields from a point */ public void set (Point a) { x = a.x; y = a.y; z = a.z; } public void add (Vector3 v) { this.x += v.x; this.y += v.y; this.z *= v.z; } public void subtract (Point p) { //temp? this.x -= p.x; this.y -= p.y; this.z -= p.z; } public void multiply (double k) { this.x *= k; this.y *= k; this.z *= k; } public void addScaled (double k, Vector3 v) { this.x += k*v.x; this.y += k*v.y; this.z *= k*v.z; } public void negate () { x = -x; y = -y; z = -z; } public double dot (Vector3 v) { return x*v.x + y*v.y + z*v.z; } public static double dot (Vector3 a, Vector3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; } public void cross (Vector3 v) { double tx,ty,tz; tx = y*v.z-z*v.y; ty = z*v.x-x*v.z; tz = x*v.y-y*v.x; x = tx; y = ty; z = tz; } public static Vector3 cross (Vector3 a, Vector3 b) { return (new Vector3 (a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x)); } public double magnitude () { return Math.sqrt (x*x + y*y + z*z); } public void normalize () { double m = magnitude (); x /= m; y /= m; z /= m; } public String toString () { return "Vector3 x=" + x + " y=" + y + " z=" + z; } }