-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
70 lines (55 loc) · 1.36 KB
/
Grid.java
File metadata and controls
70 lines (55 loc) · 1.36 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
69
70
import java.util.ArrayList;
import java.util.List;
public class Grid
{
private ArrayList<Sphere>[][] grid;
private double cellsize;
public Grid(double width, double height, double cellsize)
{
reSet(width, height, cellsize);
}
public void reSet(double width, double height, double cellsize)
{
this.cellsize = cellsize;
final int he = (int)Math.ceil(height / cellsize);
final int wi = (int)Math.ceil(width / cellsize);
grid = new ArrayList[he][wi];
for(int i = 0; i < grid.length; i++)
{
for(int k = 0; k < grid[0].length; k++)
{
grid[i][k] = new ArrayList<Sphere>();
}
}
}
public void clear(Sphere s)
{
for(ArrayList<Sphere> list : s.list)
{
list.remove(s);
}
s.list.clear();
}
public void fill(List<Sphere> slist)
{
final Vector pos = new Vector();
for(final Sphere s : slist)
{
Vector.sub_num(pos, s.pos, s.radius);
final int beginX = Math.max((int)(pos.x / cellsize), 0);
final int beginY = Math.max((int)(pos.y / cellsize), 0);
Vector.add_num(pos, pos, s.radius * 2.0);
final int endX = Math.min((int)(pos.x / cellsize), grid[0].length-1);
final int endY = Math.min((int)(pos.y / cellsize), grid.length-1);
for(int y = beginY; y <= endY; ++y)
{
for(int x = beginX; x <= endX; ++x)
{
ArrayList<Sphere> cell = grid[y][x];
cell.add(s);
s.list.add(cell);
}
}
}
}
}