Rectangle.java


This is the syntax highlighted version of Rectangle.java from 3.3 Modular Programming of
Introduction to Computer Science by Robert Sedgewick and Kevin Wayne.

/*************************************************************************
 *  Compilation:  javac Rectangle.java
 *  Execution:    java Rectangle
 *  Dependencies: Point.java
 *
 *  Implementation of an axis-aligned rectangle.
 *
 *************************************************************************/

public class Rectangle { 
    private double x1, y1;   // lower left
    private double x2, y2;   // upper right
   
    public Rectangle(double x1, double y1, double x2, double y2) {
        this.x1 = Math.min(x1, x2);
        this.x2 = Math.max(x1, x2);
        this.y1 = Math.min(y1, y2);
        this.y2 = Math.max(y1, y2);
    }

    public Rectangle(Point p, Point q) {
        this(p.x(), p.y(), q.x(), q.y());
    }

    // random rectangle in unit square
    public Rectangle() {
        this(new Point(), new Point());
    }

    // is (x, y) inside this Rectangle?
    public boolean contains(double x, double y) {
        return (x >= x1 && x <= x2 && y >= y1 && y <= y2);
    }

    // is Point p inside this Rectangle?
    public boolean contains(Point p) {
        return contains(p.x(), p.y());
    }


    // does this Rectangle r intersect s?
    public boolean intersects(Rectangle s) {
        Rectangle r = this;
        return (r.x2 >= s.x1 && r.y2 >= s.y1 && s.x2 >= r.x1 && s.y2 >= r.y1);
    }


    // return the area
    public double area() {
        return (x2 - x1) * (y2 - y1);
    }

    // return the area
    public String toString() {
        return "[(" + x1 + ", " + y1 + ") (" + x2 + ", " + y2 + ")]";
    }



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


        // generate N random points in the unit square and check 
        // fraction in the rectangle
        Rectangle rect = new Rectangle();
        System.out.println(rect);
        int yes = 0;
        for (int i = 0; i < N; i++) {
            Point p = new Point();
            if (rect.contains(p)) yes++;
        }
        System.out.println("Fraction in rectangle = " + 1.0 * yes / N);
        System.out.println("Area of rectangle     = " + rect.area());


        // check rectangle intersection
        Rectangle r0 = new Rectangle(0.25, 0.25, 0.75, 0.75);

        Rectangle r1 = new Rectangle(0.4, 0.4, 0.6, 0.6);    // r0 completely contains r1
        Rectangle r2 = new Rectangle(0.1, 0.1, 0.9, 0.9);    // r2 completely contains r0
        Rectangle r3 = new Rectangle(0.2, 0.2, 0.6, 0.6);    // intersection
        Rectangle r4 = new Rectangle(0.2, 0.2, 0.6, 0.6);    // intersection
        Rectangle r5 = new Rectangle(0.3, 0.3, 0.8, 0.5);    // intersection
        Rectangle r6 = new Rectangle(0.3, 0.3, 0.5, 0.8);    // intersection
        Rectangle r7 = new Rectangle(0.3, 0.3, 0.5, 0.8);    // intersection
        System.out.println(r0.intersects(r1));
        System.out.println(r0.intersects(r2));
        System.out.println(r0.intersects(r3));
        System.out.println(r0.intersects(r4));
        System.out.println(r0.intersects(r5));
        System.out.println(r0.intersects(r6));
        System.out.println(r0.intersects(r7));

        Rectangle r8  = new Rectangle(0.1, 0.1, 0.2, 0.2);    // no intersection
        Rectangle r9  = new Rectangle(0.1, 0.1, 0.2, 0.8);    // no intersection
        Rectangle r10 = new Rectangle(0.1, 0.1, 0.8, 0.2);    // no intersection
        System.out.println(r0.intersects(r8));
        System.out.println(r0.intersects(r9));
        System.out.println(r0.intersects(r10));
    }
}


Last updated: Sat Jun 26 14:12:37 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.