CSci2110 - Problem Set 3
Due: March 4th by 11:59pm
Geometric primitives.
To get started, implement a geometric primitive for
axis-aligned rectangles in the plane.
You can either use data type Point2D.java for points in the plane, which implements a superset of the following API, or create your own:
Now write a data type RectHV.java that implements the following API:public class Point2D implements Comparable<Point2D> { public Point2D(double x, double y) // construct the point (x, y) public double x() // x-coordinate public double y() // y-coordinate public double distanceTo(Point2D that) // Euclidean distance between two points public double distanceSquaredTo(Point2D that) // square of Euclidean distance between two points public int compareTo(Point2D that) // for use in an ordered symbol table public boolean equals(Object that) // does this point equal that? public String toString() // string representation }
Thoroughly test your data type using JUnit before proceeding.public class RectHV { public RectHV(double xmin, double ymin, // construct the rectangle [xmin, xmax] x [ymin, ymax] double xmax, double ymax) // throw an exception if (xmin > xmax) or (ymin > ymax) public double xmin() // minimum x-coordinate of rectangle public double ymin() // minimum y-coordinate of rectangle public double xmax() // maximum x-coordinate of rectangle public double ymax() // maximum y-coordinate of rectangle public boolean contains(Point2D p) // does this rectangle contain the point p (either inside or on boundary)? public boolean intersects(RectHV that) // does this rectangle intersect that rectangle (at one or more points)? public double distanceTo(Point2D p) // Euclidean distance from point p to the closest point in rectangle public double distanceSquaredTo(Point2D p) // square of Euclidean distance from point p to closest point in rectangle public boolean equals(Object that) // does this rectangle equal that? public String toString() // string representation }
Brute-force implementation. Write a data type PointSET.java that represents a set of points in the unit square. Implement the following API by using a binary search tree (using either SET or java.util.TreeSet) or a sequence..
Your implementation should support insert() and contains() in linear time (if you are using a sequence) and time proportional to the logarithm of the number of points in the set, if you are using a binary search tree; it should support nearest() and range() in time proportional to the number of points in the set.public class PointSET { public PointSET() // construct an empty set of points public boolean isEmpty() // is the set empty? public int size() // number of points in the set public void insert(Point2D p) // add the point p to the set public boolean contains(Point2D p) // does the set contain the point p? public Collection<Point2D> range(RectHV rect) // all points in the set that are inside the rectangle public Point2D nearest(Point2D p) // a nearest neighbor in the set to p; null if set is empty }
2d-tree implementation. Write a data type KdTree.java that uses a 2d-tree to implements the same API as PointSET. A 2d-tree is a generalization of a binary search tree to two-dimensional keys. The idea is to build a binary search tree with points in the nodes, using the x- and y-coordinates of the points as keys in strictly alternating sequence.
insert (0.7, 0.2) insert (0.5, 0.4) insert (0.2, 0.3) insert (0.4, 0.7) insert (0.9, 0.6)
The prime advantage of a 2d-tree over a BST is that it supports efficient implementation of range search and nearest neighbor search. Each node corresponds to an axis-aligned rectangle in the unit square, which encloses all of the points in its subtree. The root corresponds to the unit square; the left and right children of the root corresponds to the two rectangles split by the x-coordinate of the point at the root; and so forth.
Analysis. Analyze your approach to this problem giving estimates of its time and space requirements. In particular:
Submission. Submit RectHV.java, PointSET.java, and KdTree.java and any other files needed by your program.
This assignment was modified from an orignal assignment by K. Wayne.