checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Line.java
diff --git a/src/edu/berkeley/qfat/geom/Line.java b/src/edu/berkeley/qfat/geom/Line.java
new file mode 100644 (file)
index 0000000..7fb9b86
--- /dev/null
@@ -0,0 +1,55 @@
+package edu.berkeley.qfat.geom;
+import javax.media.opengl.*;
+
+/** an infinitely long line in 3-space */
+public class Line {
+
+    // y=mx+c
+    // z=nx+d
+    public final float m, n, c, d;
+
+    /** the line passing through two points */
+    public Line(Point p1, Point p2) {
+        this.m = (p2.y-p1.y)/(p2.x-p1.x);
+        this.n = (p2.z-p1.z)/(p2.x-p1.x);
+        this.c = p1.y - m * p1.x;
+        this.d = p1.z - n * p1.x;
+    }
+
+    public int hashCode() {
+        return
+            Float.floatToIntBits(m) ^
+            Float.floatToIntBits(n) ^
+            Float.floatToIntBits(c) ^
+            Float.floatToIntBits(d);
+    }
+    public boolean equals(Object o) {
+        if (o==null || !(o instanceof Line)) return false;
+        Line line = (Line)o;
+        return line.m==m && line.n==n && line.c==c && line.d==d;
+    }
+
+    public String toString() {
+        return "[line: y="+m+"x+"+c+" z="+n+"x+"+d+"]";
+    }
+
+    public double distance(Point p) { return projection(p).distance(p); }
+
+    /** returns the point on this line which is closest to p */
+    public Point projection(Point p) {
+        /*
+        Point p1 = new Point(0, c,   d);
+        Point p2 = new Point(1, m+c, n+d);
+        Vec w = p.minus(p1);
+        return getUnit().times(w.dot(getUnit()));
+        */
+        throw new RuntimeException("test this before using; may not be correct");
+    }
+
+    public Vec getUnit() {
+        Point p1 = new Point(0, c,   d);
+        Point p2 = new Point(1, m+c, n+d);
+        return p2.minus(p1).norm();
+    }
+
+}