1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
4 /** an infinitely long line in 3-space */
9 public final float m, n, c, d;
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;
19 public int hashCode() {
21 Float.floatToIntBits(m) ^
22 Float.floatToIntBits(n) ^
23 Float.floatToIntBits(c) ^
24 Float.floatToIntBits(d);
26 public boolean equals(Object o) {
27 if (o==null || !(o instanceof Line)) return false;
29 return line.m==m && line.n==n && line.c==c && line.d==d;
32 public String toString() {
33 return "[line: y="+m+"x+"+c+" z="+n+"x+"+d+"]";
36 public double distance(Point p) { return projection(p).distance(p); }
38 /** returns the point on this line which is closest to p */
39 public Point projection(Point p) {
41 Point p1 = new Point(0, c, d);
42 Point p2 = new Point(1, m+c, n+d);
44 return getUnit().times(w.dot(getUnit()));
46 throw new RuntimeException("test this before using; may not be correct");
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();