/*********************************************************** * Donna Gabai, dgabai, P01B * fall10 exam 2 * * Range2D object for 2-dimensional intervals. * All intervals are open and are in the range (0, 1). * * Dependencies: StdIn, StdOut, StdDraw, Range1D * *******************************************************/ public class Range2D { // instance variables private Range1D h; // horizontal range private Range1D v; // vertical range // constructor public Range2D(Range1D hor, Range1D vert) { // not a defensive copy h = hor; v = vert; } // Does this range intersect that? public boolean intersects(Range2D that) { // Intersects if BOTH h and v ranges intersect if (this.h.intersects(that.h) && this.v.intersects(that.v)) return true; else return false; } // Draw a rectangle that shows this range public void draw() { // find center (x, y) and halfwidth and halfheight double cx = h.mid(); double cy = v.mid(); double halfw = h.size()/2.0; double halfh = v.size()/2.0; StdDraw.filledRectangle(cx, cy, halfw, halfh); } // string representation of a range public String toString() { String s = "(" + h.toString() + ", " + v.toString() + ")"; return s; } // test main public static void main(String[] args) { // read in range pairs from standard input // assumes good data in appropriate order and quantity while(!StdIn.isEmpty()) { // first range double lo1 = StdIn.readDouble(); double hi1 = StdIn.readDouble(); Range1D r1 = new Range1D(lo1, hi1); // second range double lo2 = StdIn.readDouble(); double hi2 = StdIn.readDouble(); Range1D r2 = new Range1D(lo2, hi2); // make 2D range Range2D r2d = new Range2D(r1, r2); // output: Draw it r2d.draw(); } } }