checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Segment.java
index 8a1febd..fa303d1 100644 (file)
@@ -9,6 +9,15 @@ public class Segment implements HasBoundingBox {
 
     public Segment(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; }
 
+    public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
+    public boolean equals(Object o) {
+        if (o==null || !(o instanceof Segment)) return false;
+        Segment seg = (Segment)o;
+        if (seg.p1.equals(p1) && seg.p2.equals(p2)) return true;
+        if (seg.p2.equals(p1) && seg.p1.equals(p2)) return true;
+        return false;
+    }
+
     public float getMaxX() { return Math.max(p1.x, p2.x); }
     public float getMinX() { return Math.min(p1.x, p2.x); }
     public float getMaxY() { return Math.max(p1.y, p2.y); }
@@ -16,4 +25,18 @@ public class Segment implements HasBoundingBox {
     public float getMaxZ() { return Math.max(p1.z, p2.z); }
     public float getMinZ() { return Math.min(p1.z, p2.z); }
 
+    /** the line passing through both endpoints of this segment */
+    public Line getLine() { return new Line(p1, p2); }
+
+    public float length() { return p1.distance(p2); }
+
+    public double distance(Point p) {
+        Line line = getLine();
+        Point proj = line.projection(p);
+        if (proj.distance(p1)+proj.distance(p2) > length())
+            return Math.min(p1.distance(p),
+                            p2.distance(p));
+        return proj.distance(p);
+    }
+
 }