forked from paulrodriguez/kd-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointSET.java
74 lines (65 loc) · 1.44 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
/****
* Author: Paul Rodriguez
* Created: 3/11/2014
* Last Updated: 3/14/2014
*/
import java.util.ArrayList;
public class PointSET
{
private SET<Point2D> points;
public PointSET()
{
points = new SET<Point2D>();
}
public boolean isEmpty()
{
return points.isEmpty();
}
public int size()
{
return points.size();
}
public void insert(Point2D p)
{
points.add(p);
}
public boolean contains(Point2D p)
{
return points.contains(p);
}
public void draw()
{
for (Point2D p : points)
{
p.draw();
}
}
public Iterable<Point2D> range(RectHV rect)
{
ArrayList<Point2D> inRange = new ArrayList<Point2D>();
for (Point2D p : points)
{
if (rect.contains(p))
{
inRange.add(p);
}
}
return inRange;
}
public Point2D nearest(Point2D p)
{
Point2D minPoint = null;
double minDistance = Double.POSITIVE_INFINITY;
double distance = 0.0;
for (Point2D point : points)
{
distance = p.distanceTo(point);
if (distance < minDistance)
{
minPoint = point;
minDistance = distance;
}
}
return minPoint;
}
}