checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Vec.java
diff --git a/src/edu/berkeley/qfat/geom/Vec.java b/src/edu/berkeley/qfat/geom/Vec.java
new file mode 100644 (file)
index 0000000..c331d39
--- /dev/null
@@ -0,0 +1,18 @@
+package edu.berkeley.qfat.geom;
+
+/** vector in 3-space; immutable */
+public final class Vec {
+    public final float x, y, z;
+    public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
+    public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
+    public Vec(Point p1, Point p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
+    public Vec cross(Vec v) { return new Vec(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
+    public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
+    public Vec norm() { return mag()==0 ? this : div(mag()); }
+    public Vec times(Matrix m) { return m.apply(this); }
+    public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
+    public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
+    public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
+    public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
+    public String toString() { return "<"+x+","+y+","+z+">"; }
+}