checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Line.java
1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
3
4 /** an infinitely long line in 3-space */
5 public class Line {
6
7     // y=mx+c
8     // z=nx+d
9     public final float m, n, c, d;
10
11     /** the line passing through two points */
12     public Line(Point p1, Point p2) {
13         this.m = (p2.y-p1.y)/(p2.x-p1.x);
14         this.n = (p2.z-p1.z)/(p2.x-p1.x);
15         this.c = p1.y - m * p1.x;
16         this.d = p1.z - n * p1.x;
17     }
18
19     public int hashCode() {
20         return
21             Float.floatToIntBits(m) ^
22             Float.floatToIntBits(n) ^
23             Float.floatToIntBits(c) ^
24             Float.floatToIntBits(d);
25     }
26     public boolean equals(Object o) {
27         if (o==null || !(o instanceof Line)) return false;
28         Line line = (Line)o;
29         return line.m==m && line.n==n && line.c==c && line.d==d;
30     }
31
32     public String toString() {
33         return "[line: y="+m+"x+"+c+" z="+n+"x+"+d+"]";
34     }
35
36     public double distance(Point p) { return projection(p).distance(p); }
37
38     /** returns the point on this line which is closest to p */
39     public Point projection(Point p) {
40         /*
41         Point p1 = new Point(0, c,   d);
42         Point p2 = new Point(1, m+c, n+d);
43         Vec w = p.minus(p1);
44         return getUnit().times(w.dot(getUnit()));
45         */
46         throw new RuntimeException("test this before using; may not be correct");
47     }
48
49     public Vec getUnit() {
50         Point p1 = new Point(0, c,   d);
51         Point p2 = new Point(1, m+c, n+d);
52         return p2.minus(p1).norm();
53     }
54
55 }