-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointSET.java
More file actions
68 lines (67 loc) · 1.81 KB
/
PointSET.java
File metadata and controls
68 lines (67 loc) · 1.81 KB
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
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import java.util.TreeSet;
public class PointSET {
private final TreeSet<Point2D> sets;
public PointSET() {
sets = new TreeSet<Point2D>();
}
public boolean isEmpty() {
return sets.isEmpty();
}
public int size() {
return sets.size();
}
public void insert(Point2D node) {
if (node == null) {
throw new IllegalArgumentException();
} else {
sets.add(node);
}
}
public boolean contains(Point2D node) {
if (node == null) {
throw new IllegalArgumentException();
} else {
return sets.contains(node);
}
}
public void draw() {
for (Point2D i : sets) {
i.draw();
}
}
public Iterable<Point2D> range(RectHV rect) {
if (rect == null) {
throw new IllegalArgumentException();
} else {
TreeSet<Point2D> ret = new TreeSet<Point2D>();
for (Point2D i : sets) {
if (rect.contains(i)) ret.add(i);
}
return ret;
}
}
public Point2D nearest(Point2D node) {
if (node == null) {
throw new IllegalArgumentException();
} else {
Point2D ret = null;
double dist = 0;
double temp = 0;
for (Point2D i : sets) {
if (ret == null) {
ret = i;
dist = node.distanceSquaredTo(i);
} else {
temp = node.distanceSquaredTo(i);
if (temp < dist) {
ret = i;
dist = temp;
}
}
}
return ret;
}
}
}