<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * A class for rectangles of arbitrary dimension.
 * 
 * For HW04, CS409.
 * @author Paul Chew, Feb 2000.
 */
class Rectangle {
	
	private int[] lowerCorner, upperCorner;	// Low and high corners of this Rectangle
	
	/**
	 * Rectangle constructor.
	 * @param lowerCorner the low corner of this rectangle
	 * @param upperCorner the high corner of this rectangle
	 * @exception IllegalArgumentException if dimensions don't match or
	 * if some coordinate of the lowerCorner is above the corresponding
	 * coordinate of the upperCorner
	 */
	public Rectangle (int[] lowerCorner, int[] upperCorner) {
		if (lowerCorner.length != upperCorner.length)
			throw new IllegalArgumentException("Dimension mismatch");
		for (int i = 0; i &lt; lowerCorner.length; i++) {
			if (lowerCorner[i] &gt; upperCorner[i])
				throw new IllegalArgumentException("Corners out of order");
		}
		this.lowerCorner = lowerCorner;
		this.upperCorner = upperCorner;
	}
	
	/**
	 * Report this Rectangle's dimension.
	 * @return the dimension of this Rectangle
	 */
	public int dimension () {
		return lowerCorner.length;
	}
	
	/**
	 * Determine whether the given point is contained within this Rectangle.
	 * It is an error if the point is the wrong dimension.
	 * @param point the point to test for containment
	 * @return true iff the point is within (or on) the Rectangle
	 * @exception IllegalArgumentException if dimensions don't match
	 */
	public boolean contains (int[] point) {
		if (lowerCorner.length != point.length)
			throw new IllegalArgumentException("Dimension mismatch");
		for (int i = 0; i &lt; point.length; i++) {
			if (point[i] &lt; lowerCorner[i] || point[i] &gt; upperCorner[i])
				return false;
		}
		return true;
	}
	
	/**
	 * Report the Rectangle that is the intersection of this Rectangle
	 * and rect.  Null is returned if the Rectangles don't intersect.
	 * @param rect the Rectangle to be intersected with this Rectangle
	 * @return the intersection Rectangle of this and rect; null is returned
	 * if the rectangles don't intersect
	 * @exception IllegalArgumentException if dimensions don't match
	 */
	public Rectangle intersect (Rectangle rect) {
		int dim = this.dimension();
		if (dim != rect.dimension())
			throw new IllegalArgumentException("Dimension mismatch");
		int[] low = new int[dim];
		int[] high = new int[dim];
		for (int i = 0; i &lt; dim; i++) {
			low[i] = Math.max(this.lowerCorner[i],rect.lowerCorner[i]);
			high[i] = Math.min(this.upperCorner[i],rect.upperCorner[i]);
		}
		try {return new Rectangle(low,high);}
		catch (IllegalArgumentException e) {
			return null;
		}
	}
	
	/**
	 * Report a String representation of this Rectangle.
	 * @return a String representation of this Rectangle.
	 */
	public String toString () {
		int dim = dimension();
		if (dim == 0) return "[();()]";
		StringBuffer buf = new StringBuffer("[(");
		buf.append(lowerCorner[0]);
		for (int i = 1; i &lt; dim; i++) {
			buf.append("," + lowerCorner[i]);
		}
		buf.append(");(");
		buf.append(upperCorner[0]);
		for (int i = 1; i&lt; dim; i++) {
			buf.append("," + upperCorner[i]);
		}
		return buf.append(")]").toString();
	}
	
	/**
	 * Main program (used for testing).
	 */
	public static void main (String[] args) {
		// Overlapping intervals
		Rectangle ra = new Rectangle(new int[] {0}, new int[] {10});
		Rectangle rb = new Rectangle(new int[] {5}, new int[] {15});
		System.out.println("First Rectangle: " + ra);
		System.out.println("Second Rectangle: " + rb);
		System.out.println("Intersection: " + ra.intersect(rb));
		
		// Simple 1D containment
		System.out.println(rb + " contains 9: " + rb.contains(new int[] {9}));
		System.out.println(rb + " contains 1: " + rb.contains(new int[] {1}));
		System.out.println();
		
		// One interval within the other
		ra = new Rectangle(new int[] {0}, new int[] {20});
		rb = new Rectangle(new int[] {5}, new int[] {15});
		System.out.println("First Rectangle: " + ra);
		System.out.println("Second Rectangle: " + rb);
		System.out.println("Intersection: " + ra.intersect(rb));
		System.out.println();
		
		// Nonintersecting intervals
		ra = new Rectangle(new int[] {0}, new int[] {5});
		rb = new Rectangle(new int[] {10}, new int[] {15});
		System.out.println("First Rectangle: " + ra);
		System.out.println("Second Rectangle: " + rb);
		System.out.println("Intersection: " + ra.intersect(rb));
		System.out.println();
		
		// Intersecting cubes with some negative coordinates
		ra = new Rectangle(new int[] {-2,-2,-2}, new int[] {2,2,2});
		rb = new Rectangle(new int[] {-1,-1,-1}, new int[] {3,3,3});
		System.out.println("First Rectangle: " + ra);
		System.out.println("Second Rectangle: " + rb);
		System.out.println("Intersection: " + ra.intersect(rb));
		
		// Simple 3D containment (note: points on boundary are contained)
		System.out.println(rb + " contains (1,2,3): " + rb.contains(new int[] {1,2,3}));
		System.out.println(rb + " contains (2,2,2): " + rb.contains(new int[] {2,2,2}));
		System.out.println(rb + " contains (3,3,3): " + rb.contains(new int[] {3,3,3}));
		System.out.println(rb + " contains (4,2,3): " + rb.contains(new int[] {4,2,3}));
		System.out.println(rb + " contains (4,5,6): " + rb.contains(new int[] {4,5,6}));
		System.out.println();
		
		// Nonintersecting cubes with some negative coordinates
		ra = new Rectangle(new int[] {0,0,0}, new int[] {1,1,1});
		rb = new Rectangle(new int[] {-2,-2,-2}, new int[] {-1,-1,-1});
		System.out.println("First Rectangle: " + ra);
		System.out.println("Second Rectangle: " + rb);
		System.out.println("Intersection: " + ra.intersect(rb));
		System.out.println();
		
		// Barely intersecting 4D bricks
		ra = new Rectangle(new int[] {0,0,0,1}, new int[] {1,1,1,2});
		rb = new Rectangle(new int[] {1,1,1,1}, new int[] {2,2,2,2});
		System.out.println("First Rectangle: " + ra);
		System.out.println("Second Rectangle: " + rb);
		System.out.println("Intersection: " + ra.intersect(rb));
		System.out.println();
	}
		
}</pre></body></html>