-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointSET.java
101 lines (84 loc) · 3.48 KB
/
PointSET.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdOut;
import java.util.Set;
import java.util.TreeSet;
public class PointSET {
private final TreeSet<Point2D> pointTreeSet;
public PointSET() // construct an empty set of points
{
pointTreeSet = new TreeSet<Point2D>();
}
public boolean isEmpty() // is the set empty?
{
return pointTreeSet.isEmpty();
}
public int size() // number of points in the set
{
return pointTreeSet.size();
}
public void insert(
Point2D p) // add the point to the set (if it is not already in the set)
{
if (p == null) throw new IllegalArgumentException("Null argument for method insert");
pointTreeSet.add(p);
}
public boolean contains(Point2D p) // does the set contain point p?
{
if (p == null) throw new IllegalArgumentException("Null argument for method contains");
return pointTreeSet.contains(p);
}
public void draw() // draw all points to standard draw
{
for (Point2D point : pointTreeSet) {
point.draw();
}
}
public Iterable<Point2D> range(
RectHV rect) // all points that are inside the rectangle (or on the boundary)
{
if (rect == null) throw new IllegalArgumentException("Null argument for method range");
Stack<Point2D> rangePoints = new Stack<Point2D>();
Set<Point2D> pointsInY = pointTreeSet.subSet(new Point2D(rect.xmin(), rect.ymin()), true,
new Point2D(rect.xmax(), rect.ymax()), true);
for (Point2D p : pointsInY) {
if (p.x() <= rect.xmax() && p.x() >= rect.xmin()) rangePoints.push(p);
}
return rangePoints;
}
public Point2D nearest(
Point2D p) // a nearest neighbor in the set to point p; null if the set is empty
{
if (p == null) throw new IllegalArgumentException("Null argument for method nearest");
double minDistanceSquare = Double.POSITIVE_INFINITY;
double currDistanceSquare;
Point2D neighborPoint = null;
for (Point2D point : pointTreeSet) {
currDistanceSquare = p.distanceSquaredTo(point);
if (currDistanceSquare < minDistanceSquare) {
minDistanceSquare = currDistanceSquare;
neighborPoint = point;
}
}
return neighborPoint;
}
public String toString() {
return pointTreeSet.toString();
}
public static void main(
String[] args) // unit testing of the methods (optional)
{
PointSET myPointSet = new PointSET();
StdOut.printf("Now our tree is empty: %b\n", myPointSet.isEmpty());
myPointSet.insert(new Point2D(0.2, 0.5));
myPointSet.insert(new Point2D(0.4, 0.5));
myPointSet.insert(new Point2D(0.5, 0.5));
myPointSet.insert(new Point2D(0.6, 0.5));
myPointSet.draw();
Iterable<Point2D> pointsInRect = myPointSet.range(new RectHV(0.1, 0.2, 0.4, 0.5));
for (Point2D point : pointsInRect) StdOut.println(point);
Iterable<Point2D> pointsInRect2 = myPointSet.range(new RectHV(0.1, 0.4, 0.3, 0.5));
for (Point2D point : pointsInRect2) StdOut.println(point);
}
}