2
2
#define ACTRQUADTREE_H
3
3
#include "actralloc.h"
4
4
#include "actrvector.h"
5
+ #include "actrcanvas.h"
6
+
7
+ extern void _actr_debug_length (char * ptr , int len , int val );
8
+ void actr_debug (char * ptr , int val )
9
+ {
10
+ _actr_debug_length (ptr , strlen (ptr ), val );
11
+ }
5
12
struct ActrQuadTreeBounds
6
13
{
7
14
long top ;
@@ -23,7 +30,6 @@ struct ActrQuadTree
23
30
struct ActrQuadTree * three ;
24
31
struct ActrQuadTree * four ;
25
32
struct ActrVector * items ;
26
- int itemCount ;
27
33
};
28
34
29
35
struct ActrQuadTreeBounds * _actr_quad_tree_bounds (long top , long right , long bottom , long left )
@@ -49,19 +55,37 @@ struct ActrQuadTree *actr_quad_tree_init()
49
55
{
50
56
struct ActrQuadTree * result = actr_malloc (sizeof (struct ActrQuadTree ));
51
57
result -> root = 1 ;
52
- result -> bounds = _actr_quad_tree_bounds (64 , 64 , 64 , 64 );
58
+ result -> bounds = _actr_quad_tree_bounds (0 , 64 , 64 , 0 );
53
59
return result ;
54
60
}
55
61
int _actr_quad_tree_bounds_contains (struct ActrQuadTreeBounds * bounds , struct ActrQuadTreeBounds * other )
56
62
{
63
+
57
64
if (other -> top < bounds -> top )
65
+ {
66
+ actr_debug ("1contains one" , other -> top );
67
+ actr_debug ("2contains one" , bounds -> top );
58
68
return 0 ;
69
+ }
70
+ // 566 > 0
59
71
if (other -> right > bounds -> right )
72
+ {
73
+ actr_debug ("1contains two" , other -> right );
74
+ actr_debug ("2contains two" , bounds -> right );
60
75
return 0 ;
76
+ }
61
77
if (other -> bottom > bounds -> bottom )
78
+ {
79
+ actr_debug ("1contains three" , other -> bottom );
80
+ actr_debug ("2contains three" , bounds -> bottom );
62
81
return 0 ;
82
+ }
63
83
if (other -> left < bounds -> left )
84
+ {
85
+ actr_debug ("1contains four" , other -> left );
86
+ actr_debug ("2contains four" , bounds -> left );
64
87
return 0 ;
88
+ }
65
89
return 1 ;
66
90
}
67
91
// 1 2
@@ -70,7 +94,9 @@ int _actr_quad_tree_bounds_contains(struct ActrQuadTreeBounds *bounds, struct Ac
70
94
void _actr_quad_tree_grow (struct ActrQuadTree * tree )
71
95
{
72
96
long size = tree -> bounds -> right - tree -> bounds -> left ;
97
+ actr_debug ("grow size" , size );
73
98
long grow = (size ) / 2 ;
99
+ actr_debug ("grow grow" , grow );
74
100
struct ActrQuadTree * new ;
75
101
if (tree -> one )
76
102
{
@@ -154,13 +180,50 @@ int _actr_quad_tree_index(struct ActrQuadTree *tree, struct ActrQuadTreeBounds *
154
180
}
155
181
return 3 ;
156
182
}
183
+ void actr_quad_tree_draw (struct ActrQuadTree * tree )
184
+ {
185
+ struct ActrQuadTreeLeaf * leaf ;
186
+ if (tree -> items )
187
+ {
188
+ for (int i = 0 ; i < tree -> items -> pointer ; i ++ )
189
+ {
190
+ leaf = * (struct ActrQuadTreeLeaf * * )(tree -> items -> head + i * sizeof (void * ));
191
+ actr_canvas2d_stroke_rect (leaf -> bounds -> left - 0.5 , leaf -> bounds -> top - 0.5 , leaf -> bounds -> right - leaf -> bounds -> left , leaf -> bounds -> bottom - leaf -> bounds -> top );
192
+ }
193
+ }
194
+ }
157
195
void actr_quad_tree_insert (struct ActrQuadTree * tree , long top , long right , long bottom , long left , void * item )
158
196
{
159
197
struct ActrQuadTreeLeaf * leaf = _actr_quad_tree_leaf (top , right , bottom , left , item );
160
- while (!_actr_quad_tree_bounds_contains (tree -> bounds , leaf -> bounds ))
198
+ if (tree -> root )
199
+ {
200
+ while (!_actr_quad_tree_bounds_contains (tree -> bounds , leaf -> bounds ))
201
+ {
202
+ _actr_quad_tree_grow (tree );
203
+ }
204
+ }
205
+ if (!tree -> items )
161
206
{
162
- _actr_quad_tree_grow (tree );
207
+ tree -> items = actr_malloc (sizeof (struct ActrVector ));
208
+ actr_vector_add (tree -> items , leaf );
209
+ return ;
210
+ } else if (tree -> items ) {
211
+ actr_vector_add (tree -> items , leaf );
163
212
}
164
213
int index = _actr_quad_tree_index (tree , leaf -> bounds );
214
+
215
+ if (index == 1 )
216
+ {
217
+ }
218
+ else if (index == 2 )
219
+ {
220
+ }
221
+ else if (index == 3 )
222
+ {
223
+ }
224
+ else if (index == 4 )
225
+ {
226
+ }
227
+ actr_debug ("qt insert index" , index );
165
228
}
166
229
#endif
0 commit comments