-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadTree.cs
230 lines (183 loc) · 6.82 KB
/
QuadTree.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System.Numerics;
using Raylib_cs;
static class Constants
{
public const int MAX_SUBDIVISIONS = 8;
}
namespace QuadtreeImplementation
{
public class Point
{
public int Radius;
public Color Color;
public int X;
public int Y;
public Vector2 Position;
public static int Id;
public Point() { }
public Point(int radius, Color color, int x, int y)
{
Radius = radius;
Color = color;
X = x;
Y = y;
Position = new Vector2(X, Y);
Id++;
Console.WriteLine("Id " + Id);
}
}
public class QuadTree
{
private Rectangle _bounds;
private int _capacity;
private List<Point> _points;
private bool _divided;
private QuadTree? _topLeft;
private QuadTree? _topRight;
private QuadTree? _bottomRight;
private QuadTree? _bottomLeft;
private Color _color;
private static int subdivisionCount = 0;
public List<Point> Points { get { return _points; } }
public QuadTree(Rectangle shape, Color col)
{
_capacity = 4;
_divided = false;
_bounds = shape;
_points = new List<Point>();
_topLeft = null;
_topRight = null;
_bottomRight = null;
_bottomLeft = null;
_color = col;
}
// Add the node to the quad tree
public void Insert(Point p)
{
if (!ContainsPoint(p)) return;
if (_points.Count < _capacity)
{
_points.Add(p);
Console.WriteLine($"Points count: {_points.Count}");
}
else
{
if (_topLeft == null)
{
Subdivide();
}
_topLeft?.Insert(p);
_topRight?.Insert(p);
_bottomLeft?.Insert(p);
_bottomRight?.Insert(p);
}
}
public void Draw()
{
Raylib.DrawRectangle((int)_bounds.X, (int)_bounds.Y, (int)_bounds.Width, (int)_bounds.Height, _color);
if (_topLeft == null) return;
_topLeft?.Draw();
_topRight?.Draw();
_bottomRight?.Draw();
_bottomLeft?.Draw();
}
/*
* -> +x
* min
* ._________
* | | | +y
* | | v
* | |
* |__________. max
*
*/
public bool ContainsPoint(Point p)
{
//int pX = p.Radius + p.X;
//int pY = p.Radius + p.Y;
//Vector2 min = new(_bounds.X, _bounds.Y);
//Vector2 max = new(_bounds.X + _bounds.Width, _bounds.Y + _bounds.Height);
//if (pX > min.X && pX < max.X)
//{
// if (pY > min.Y && pY < max.Y)
// {
// return true;
// }
//}
//return false;
Console.WriteLine($"Point location: {p.X}, {p.Y}");
Console.WriteLine($"Checking in boundaries \n" +
$"Lower bounds: {_bounds.X}, {_bounds.Y}\n" +
$"Upper bounds: {_bounds.X + _bounds.Width}, {_bounds.Y + _bounds.Height}");
return Raylib.CheckCollisionPointRec(new(p.X, p.Y), _bounds);
}
// Get the new width and height by dividing both by 2
// The new X and Y coords are based on the current X and Y coords, plus the new width and new height respectively
// Create new Quadtrees, passing in rectangles with X, Y, Width, Height values from the calculated ones
/*
*
*
* *----------------------------
* | x, y | x + nw, y |
* | | |
* | | |
* |------------|-----------------
* | x, y+nh | x+nw, y+nh |
* | | |
* *----------------------------*
*
*/
private void Subdivide()
{
if (subdivisionCount < Constants.MAX_SUBDIVISIONS)
{
int newWidth = (int)_bounds.Width / 2;
int newHeight = (int)_bounds.Height / 2;
int currX = (int)_bounds.X;
int currY = (int)_bounds.Y;
int newX = currX + newWidth;
int newY = currY + newHeight;
_topLeft = new QuadTree(
new Rectangle(currX, currY, new(newWidth, newHeight)),
new(new Random().Next(0, 256), new Random().Next(0, 256), new Random().Next(0, 256), 255));
_topRight = new QuadTree(new Rectangle(newX, currY, new(newWidth, newHeight)),
new(new Random().Next(0, 256), new Random().Next(0, 256), new Random().Next(0, 256), 255));
_bottomRight = new QuadTree(new Rectangle(currX, newY, new(newWidth, newHeight)),
new(new Random().Next(0, 256), new Random().Next(0, 256), new Random().Next(0, 256), 255));
_bottomLeft = new QuadTree(new Rectangle(newX, newY, new(newWidth, newHeight)),
new(new Random().Next(0, 256), new Random().Next(0, 256), new Random().Next(0, 256), 255));
// ++subdivisionCount;
// Console.WriteLine($"Subdivision count: {subdivisionCount}");
} else
{
Console.WriteLine("Max subdivisions!");
}
}
private bool IntersectAABB(Rectangle other)
{
return Raylib.CheckCollisionRecs(_bounds, other);
}
// Check to make sure that the node is inside a given range
public List<Point> Search(Rectangle range)
{
List<Point> pointsInRange = [];
if (!IntersectAABB(range))
{
return pointsInRange;
}
foreach (Point point in _points)
{
if (Raylib.CheckCollisionPointRec(new(point.X, point.Y), range))
{
pointsInRange.Add(point);
}
}
if (_topLeft == null) return pointsInRange;
pointsInRange.AddRange(_topLeft.Search(range));
pointsInRange.AddRange(_topRight.Search(range));
pointsInRange.AddRange(_bottomRight.Search(range));
pointsInRange.AddRange(_bottomLeft.Search(range));
return pointsInRange;
}
}
}