BoundingBox.java


Below is the syntax highlighted version of BoundingBox.java from §3.5 Inheritance.


/******************************************************************************
 *  Compilation:  javac BoundingBox.java
 *  Execution:    java BoundingBox N
 *  Dependencies: Interval.java Point.java
 *
 *  Immutable data type for a bounding box (axis-aligned rectangle).
 *
 ******************************************************************************/

public class BoundingBox {
    private Interval intervalX;  // x-axis interval
    private Interval intervalY;  // y-axis interval

    // smallest bounding box containing (x1, y1) and (x2, y2)
    public BoundingBox(Point p1, Point p2) {
        intervalX = new Interval(Math.min(p1.x(), p2.x()), Math.max(p1.x(), p2.x()));
        intervalY = new Interval(Math.min(p1.y(), p2.y()), Math.max(p1.y(), p2.y()));
    }

    // smallest bounding box containing the two points
    public BoundingBox(Interval x, Interval y) {
        intervalX = x;
        intervalY = y;
    }

    // bounding box from input stream
    public BoundingBox(In in) {
        Point p1 = new Point(in);
        Point p2 = new Point(in);
        intervalX = new Interval(Math.min(p1.x(), p2.x()), Math.max(p1.x(), p2.x()));
        intervalY = new Interval(Math.min(p1.y(), p2.y()), Math.max(p1.y(), p2.y()));
    }

    // smallest bounding box containing all the points
    public BoundingBox(Point[] points) {
        double xmin = Double.POSITIVE_INFINITY;
        double ymin = Double.POSITIVE_INFINITY;
        double xmax = Double.NEGATIVE_INFINITY;
        double ymax = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < points.length; i++) {
            if (points[i].x() < xmin) xmin = points[i].x();
            if (points[i].y() < ymin) ymin = points[i].y();
            if (points[i].x() > xmax) xmax = points[i].x();
            if (points[i].y() > ymax) ymax = points[i].y();
        }
        intervalX = new Interval(xmin, xmax);
        intervalY = new Interval(ymin, ymax);
    }

    // return a random point in bounding box
    public Point sample() {
        double x = intervalX.sample();
        double y = intervalY.sample();
        return new Point(x, y);
    }

    // is (x, y) inside this BoundingBox?
    public boolean contains(double x, double y) {
        return intervalX.contains(x) && intervalY.contains(y);
    }

    // does this BoundingBox intersect b?
    public boolean intersects(BoundingBox b) {
        return intervalX.intersects(b.intervalX) &&
               intervalY.intersects(b.intervalY);
    }

    // smallest bounding box containing this bounding box and b
    public BoundingBox union(BoundingBox b) {
        return new BoundingBox(intervalX.union(b.intervalX),
                               intervalY.union(b.intervalY));
    }

    // return the width, height and area
    public double width()  { return intervalX.length(); }
    public double height() { return intervalY.length(); }
    public double area()   { return width() * height(); }

    // return a string representation
    public String toString() {
        return "[ " + intervalX + " " + intervalY + " ]";
    }


    // test client
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);


        // check rectangle intersection
        BoundingBox r0 = new BoundingBox(new Point(0.25, 0.25), new Point(0.75, 0.75));
        BoundingBox r1 = new BoundingBox(new Point(0.4, 0.4), new Point(0.6, 0.6));    // r0 completely contains r1
        BoundingBox r2 = new BoundingBox(new Point(0.1, 0.1), new Point(0.9, 0.9));    // r2 completely contains r0
        BoundingBox r3 = new BoundingBox(new Point(0.2, 0.2), new Point(0.6, 0.6));    // intersection
        BoundingBox r4 = new BoundingBox(new Point(0.2, 0.2), new Point(0.6, 0.6));    // intersection
        BoundingBox r5 = new BoundingBox(new Point(0.3, 0.3), new Point(0.8, 0.5));    // intersection
        BoundingBox r6 = new BoundingBox(new Point(0.3, 0.3), new Point(0.5, 0.8));    // intersection
        BoundingBox r7 = new BoundingBox(new Point(0.3, 0.3), new Point(0.5, 0.8));    // intersection
        StdOut.println(r0.intersects(r1));
        StdOut.println(r0.intersects(r2));
        StdOut.println(r0.intersects(r3));
        StdOut.println(r0.intersects(r4));
        StdOut.println(r0.intersects(r5));
        StdOut.println(r0.intersects(r6));
        StdOut.println(r0.intersects(r7));

        BoundingBox r8  = new BoundingBox(new Point(0.1, 0.1), new Point(0.2, 0.2));    // no intersection
        BoundingBox r9  = new BoundingBox(new Point(0.1, 0.1), new Point(0.2, 0.8));    // no intersection
        BoundingBox r10 = new BoundingBox(new Point(0.1, 0.1), new Point(0.8, 0.2));    // no intersection
        StdOut.println(r0.intersects(r8));
        StdOut.println(r0.intersects(r9));
        StdOut.println(r0.intersects(r10));
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:27:11 EDT 2022.