-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadTreeNode.cs
86 lines (71 loc) · 2.13 KB
/
QuadTreeNode.cs
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
using DataStructure;
public class QuadTreeNode
{
private const int Capacity = 4;
public Rectangle Boundary { get; private set; }
public List<Point> Points { get; private set; }
public bool IsDivided { get; private set; }
public QuadTreeNode NE { get; private set; }
public QuadTreeNode NW { get; private set; }
public QuadTreeNode SE { get; private set; }
public QuadTreeNode SW { get; private set; }
public QuadTreeNode(Rectangle boundary)
{
Boundary = boundary;
Points = new List<Point>();
IsDivided = false;
}
public bool Insert(Point point)
{
if (!Boundary.Contains(point))
return false;
if (Points.Count < Capacity)
{
Points.Add(point);
return true;
}
else
{
if (!IsDivided)
Subdivide();
if (NE.Insert(point)) return true;
if (NW.Insert(point)) return true;
if (SE.Insert(point)) return true;
if (SW.Insert(point)) return true;
}
return false;
}
private void Subdivide()
{
System.Double x = Boundary.X;
System.Double y = Boundary.Y;
System.Double w = Boundary.Width / 2;
System.Double h = Boundary.Height / 2;
Rectangle ne = new Rectangle(x + w, y - h, w, h);
NE = new QuadTreeNode(ne);
Rectangle nw = new Rectangle(x - w, y - h, w, h);
NW = new QuadTreeNode(nw);
Rectangle se = new Rectangle(x + w, y + h, w, h);
SE = new QuadTreeNode(se);
Rectangle sw = new Rectangle(x - w, y + h, w, h);
SW = new QuadTreeNode(sw);
IsDivided = true;
}
public void Query(Rectangle range, List<Point> found)
{
if (!Boundary.Intersects(range))
return;
foreach (var point in Points)
{
if (range.Contains(point))
found.Add(point);
}
if (IsDivided)
{
NE.Query(range, found);
NW.Query(range, found);
SE.Query(range, found);
SW.Query(range, found);
}
}
}