/
OIGrid.java
188 lines (147 loc) · 5.28 KB
/
OIGrid.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package naybur.grid;
import static naybur.Utility.*;
import naybur.Point;
import java.util.Comparator;
import java.util.Collections;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
public class OIGrid
{
//grid is an array of linked lists. The linked lists hold Points in point_list.
//made public so that
public List<List<List<Point>>> grid;
//range is assumed to be a 2x2 array representing the area that the points under consideration occupy.
//e.g. [[0,0][100,200]] is a rectangle with top left corner at 0,0 and bottom right at [100,200]
private double[][] range;
//delta represents the size of a grid square
//diff is the absolute value of the longest dimension of the range.
private double delta, diff;
private int num_grids;
public ArrayList<Point> point_list;
public OIGrid(ArrayList<Point> points, double[][] range)
{
double xdiff = Math.abs(range[0][0] - range[0][1]);
double ydiff = Math.abs(range[1][0] - range[1][1]);
double diff = xdiff > ydiff ? xdiff : ydiff;
// println(diff);
delta = 1/Math.sqrt(points.size()) * diff;
// println(delta);
num_grids = (int)Math.ceil((1/delta)*diff);
// println(num_grids);
grid = new ArrayList<List<List<Point>>>(num_grids);
for(int i = 0; i < num_grids; i++)
{
List<List<Point>> row = new ArrayList<List<Point>>(num_grids);
grid.add(row);
for(int j = 0; j < num_grids; j++)
{
List<Point> col = new LinkedList<Point>();
row.add(col);
}
}
this.point_list = points;
this.range = range;
overhaul();
}
public void overhaul()
{
// println(delta);
for(List<List<Point>> row : grid)
{
for(List<Point> col : row)
{
col.clear();
}
}
// println(" ");
for(Point point : point_list)
{
int cell_index_x = (int)Math.floor(point.x / delta);
int cell_index_y = (int)Math.floor(point.y / delta);
// println(i+ ": " + point.x + " " + point.y);
// println(i+ ": "+ cell_index_x + " " + cell_index_y);
grid.get(cell_index_x).get(cell_index_y).add(point);
}
}
public List<Point> findNearest(Point search_point, int k)
{
int[] search_cell = { (int)Math.floor(search_point.x/delta), (int)Math.floor(search_point.y/delta) } ;
// println(search_cell[0] + " " + search_cell[1]);
// println(search_point);
Rect rect = new Rect(0, search_cell);
List<Point> neighbors = rect.findPoints(k);
// println(neighbors);
Collections.sort(neighbors, new MyComparator(search_point));
// println(neighbors);
Point farthest_neighbor = neighbors.get(neighbors.size()-1);
double farthest_neighbor_dist = sqDist(search_point, farthest_neighbor);
farthest_neighbor_dist = Math.sqrt(farthest_neighbor_dist);
int new_len = (int)Math.ceil(farthest_neighbor_dist/delta);
// println(delta);
// println(new_len);
rect = new Rect(new_len, search_cell);
neighbors = rect.findPoints(k);
Collections.sort(neighbors, new MyComparator(search_point));
// println(neighbors);
return neighbors.subList(0,k);
}
private class MyComparator implements Comparator<Point>
{
Point search_point;
public MyComparator(Point search_point)
{
this.search_point = search_point;
}
@Override
public int compare(Point a, Point b)
{
double dist_a = sqDist(a, search_point);
double dist_b = sqDist(b, search_point);
if(dist_a > dist_b)
return 1;
if(dist_a < dist_b)
return -1;
return 0;
}
}
private class Rect
{
private int len;
private int[] cent;
public Rect(int len, int[] cent)
{
this.len = len;
this.cent = cent;
}
public List<Point> findPoints(int k)
{
List<Point> neighbors = new ArrayList<Point>();
int width = grid.get(0).size();
int height = grid.size();
// println("**********************************************");
while(neighbors.size() < k)
{
neighbors.clear();
// println(len);
int start_x = cent[0] - len < 0 ? 0 : cent[0] - len;
int end_x = cent[0] + len >= width ? width-1 : cent[0] + len;
int start_y = cent[1] - len < 0 ? 0 : cent[1] - len;
int end_y = cent[1] + len >= height ? height-1 : cent[1] + len;
// println(" ");
for(int i = start_x; i <= end_x; i++)
{
for(int j = start_y; j <= end_y; j++)
{
// println(i + " " + j);
// println(grid.get(i).get(j));
neighbors.addAll(grid.get(i).get(j));
}
}
len++;
}
// println(neighbors);
return neighbors;
}
}
}