1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
4 /** a line segment defined by two points in space */
5 public class Segment implements HasBoundingBox {
10 public Segment(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; }
12 public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
13 public boolean equals(Object o) {
14 if (o==null || !(o instanceof Segment)) return false;
15 Segment seg = (Segment)o;
16 if (seg.p1.equals(p1) && seg.p2.equals(p2)) return true;
17 if (seg.p2.equals(p1) && seg.p1.equals(p2)) return true;
21 public float getMaxX() { return Math.max(p1.x, p2.x); }
22 public float getMinX() { return Math.min(p1.x, p2.x); }
23 public float getMaxY() { return Math.max(p1.y, p2.y); }
24 public float getMinY() { return Math.min(p1.y, p2.y); }
25 public float getMaxZ() { return Math.max(p1.z, p2.z); }
26 public float getMinZ() { return Math.min(p1.z, p2.z); }
28 /** the line passing through both endpoints of this segment */
29 public Line getLine() { return new Line(p1, p2); }
31 public float length() { return p1.distance(p2); }
33 public double distance(Point p) {
34 Line line = getLine();
35 Point proj = line.getProjection(p);
36 if (proj.distance(p1)+proj.distance(p2) > length())
37 return Math.min(p1.distance(p),
39 return proj.distance(p);