-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPointSET.java
118 lines (96 loc) · 2.83 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
public class PointSET {
private TreeSet<Point2D> points;
/**
* construct an empty set of points
*/
public PointSET() {
points = new TreeSet<>();
}
/**
* is the set empty?
*/
public boolean isEmpty() {
return points.isEmpty();
}
/**
* number of points in the set
*/
public int size() {
return points.size();
}
/**
* add the point to the set (if it is not already in the set)
*/
public void insert(Point2D p) {
points.add(p);
}
/**
* does the set contain point p?
*/
public boolean contains(Point2D p) {
return points.contains(p);
}
/**
* draw all points to standard draw
*/
public void draw() {
// Copied from the answer of the question in FAQ:
// "How should I set the size and color of the points and rectangles when drawing?"
// http://coursera.cs.princeton.edu/algs4/checklists/kdtree.html
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
for (Point2D p : points) {
p.draw();
}
}
/**
* all points that are inside the rectangle (or on the boundary)
*/
public Iterable<Point2D> range(RectHV rect) {
List<Point2D> ranges = new ArrayList<>();
for (Point2D p : points) {
if (rect.contains(p)) {
ranges.add(p);
}
}
return ranges;
}
/**
* a nearest neighbor in the set to point p; null if the set is empty
*/
public Point2D nearest(Point2D p) {
double minDist = Double.MAX_VALUE;
Point2D nearestPoint = null;
for (Point2D otherPoint : points) {
double dist = otherPoint.distanceTo(p);
if (dist < minDist) {
minDist = dist;
nearestPoint = otherPoint;
}
}
return nearestPoint;
}
/**
* unit testing of the methods (optional)
*/
public static void main(String[] args) {
PointSET set = new PointSET();
Point2D p1 = new Point2D(0.271484, 0.195313);
Point2D p2 = new Point2D(0.744141, 0.136719);
Point2D p3 = new Point2D(0.781250, 0.398438);
Point2D p4 = new Point2D(0.535156, 0.593750);
set.insert(p1);
set.insert(p2);
set.insert(p3);
set.insert(p4);
System.out.println("set.contains(p1) = " + set.contains(p1));
System.out.println("set.contains(p2) = " + set.contains(p2));
System.out.println("set.range(new RectHV(0.3, 0.2, 0.8, 0.8)) = " + set.range(new RectHV(0.3, 0.2, 0.8, 0.8)));
}
}