// Element of Ray Diagram // David Eppstein, UC Irvine, 18 May 1997 import java.lang.*; public class RayDiagObj extends DistancedObject { public double x, y, dx, dy, t; public RayDiagObj(double ix, double iy, double idx, double idy) { x = ix; y = iy; dx = idx; dy = idy; t = Double.POSITIVE_INFINITY; } // For what value of t is this(t) on line of ray r? // Returns infinity if no crossing exists public double crosstime(RayDiagObj r) { double det = dx*r.dy - r.dx*dy; if (det == 0) return Double.POSITIVE_INFINITY; double ct = (r.dy*(r.x-x) - r.dx*(r.y-y))/det; if (ct < 0.0 || ct >= t) return Double.POSITIVE_INFINITY; return ct; } // Distance function is latest of two crossing times public double distanceTo(DistancedObject d) { if (!(d instanceof RayDiagObj)) return Double.POSITIVE_INFINITY; RayDiagObj r = (RayDiagObj) d; double dr = crosstime(r); double rd = r.crosstime(this); if (dr < rd) return rd; else return dr; } }