/** * Does this interval equal the other interval? * @param other the other interval * @return true if this interval equals the other interval; false otherwise */ public boolean equals(Object other) { if (other == this) return true; if (other == null) return false; if (other.getClass() != this.getClass()) return false; Interval2D that = (Interval2D) other; return this.x.equals(that.x) && this.y.equals(that.y); }
/** * Unit tests the {@code Interval2D} data type. * * @param args the command-line arguments */ public static void main(String[] args) { double xmin = Double.parseDouble(args[0]); double xmax = Double.parseDouble(args[1]); double ymin = Double.parseDouble(args[2]); double ymax = Double.parseDouble(args[3]); int trials = Integer.parseInt(args[4]); Interval1D xInterval = new Interval1D(xmin, xmax); Interval1D yInterval = new Interval1D(ymin, ymax); Interval2D box = new Interval2D(xInterval, yInterval); box.draw(); Counter counter = new Counter("hits"); for (int t = 0; t < trials; t++) { double x = StdRandom.uniform(0.0, 1.0); double y = StdRandom.uniform(0.0, 1.0); Point2D point = new Point2D(x, y); if (box.contains(point)) counter.increment(); else point.draw(); } StdOut.println(counter); StdOut.printf("box area = %.2f\n", box.area()); }
/** * Does this two-dimensional interval intersect that two-dimensional interval? * @param that the other two-dimensional interval * @return true if this two-dimensional interval intersects * that two-dimensional interval; false otherwise */ public boolean intersects(Interval2D that) { if (!this.x.intersects(that.x)) return false; if (!this.y.intersects(that.y)) return false; return true; }