-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadTree.cpp
More file actions
770 lines (735 loc) · 21.3 KB
/
QuadTree.cpp
File metadata and controls
770 lines (735 loc) · 21.3 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#include "stdafx.h"
#include <cstdlib>
#include "QuadTree.h"
#include "QTQuad.h"
#include "Point.h"
#include "BBox.h"
#include <iterator>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
QuadTree::QuadTree(const BBox &bounds) //create QuadTree
{
std::cout << "Quad Tree Constructor" << endl;
m_root = new QTQuad();
m_qBounds = BBox(bounds.m_bL, bounds.m_dim);
}
QuadTree::QuadTree() //create the quadtree
{
cout << "Quad Tree Constructor" << endl;
m_root = new QTQuad();
m_qBounds = BBox(Point(0, 0), 16);
}
QuadTree::iterator::iterator()
{
cout << "GOT to iterator constructor" << endl;
QTQuad * curr = NULL;
QTQuad * currCell = NULL;
cout << "finished consturctor!" << endl;
}
/*QuadTree::iterator::iterator(QuadTree *Q)
{
Q->num = 42;
QTQuad* curr = NULL; //pointer to QTQuad
int* currCell = NULL; //pointer to m_cell
}*/
QuadTree::iterator::~iterator()
{
delete curr;
delete currCell;
}
void QuadTree::iterator::printNum(QuadTree *Q)
{
cout << Q->num << endl;
}
QuadTree::iterator QuadTree::begin()
{
cout << "Iterator Begin Running" << endl;
iterator it=iterator();
it.curr = m_root; //start with root
int dim_x_left = (int)m_qBounds.m_bL.m_x; //initial dim_x_left is equal to the left wall
int dim_x_right = (int)m_qBounds.m_bL.m_x + (int)m_qBounds.m_dim; // ditto right wall
int dim_y_top = (int)m_qBounds.m_bL.m_y + (int)m_qBounds.m_dim; //ditto top wall
int dim_y_bot = (int)m_qBounds.m_bL.m_y; //ditto bottom wall
cout << "CHIMP" << endl;
cout << "ENTER BEGIN LOOP" << endl;
while (it.curr->m_quads[0] != NULL || it.curr->m_quads[1] != NULL || it.curr->m_quads[2] != NULL || it.curr->m_quads[3] != NULL
|| it.curr->parent != NULL) //keep going unless all children are null or parent is null
{
//cout << "CHIMP" << endl;
if (it.curr->m_quads[0] != NULL) //if first is not null, send pointer there
{
dim_x_right = (dim_x_right+dim_x_left) / 2; //update wall
dim_y_top = (dim_y_bot+dim_y_top) / 2; //update top wall
it.curr = it.curr->m_quads[0]; //send pointer there
it.curr->location = 0; //QTQuad Locator
}
else if (it.curr->m_quads[1] != NULL) //and so on
{
dim_x_left = (dim_x_left+dim_x_right)/ 2; //update left wall
dim_y_top = (dim_y_top+dim_y_bot) / 2; //update top wall
it.curr = it.curr->m_quads[1]; //send pointer there
it.curr->location = 1; //location
}
else if (it.curr->m_quads[2] != NULL)
{
dim_x_right = (dim_x_right+dim_x_left) / 2;
dim_y_bot = (dim_y_top+dim_y_bot) / 2;
it.curr = it.curr->m_quads[2];
it.curr->location = 2;
}
else if (it.curr->m_quads[3] != NULL)
{
dim_x_left = (dim_x_right+dim_x_left) / 2;
dim_y_bot = (dim_y_bot+dim_y_top) / 2;
it.curr = it.curr->m_quads[3];
it.curr->location = 3;
}
}
if (it.curr->parent == NULL)
{
it.curr = m_root;
it.currCell = 0;
}
cout << "dim_x_left"<<dim_x_left << endl;
cout << "dim_x_right"<<dim_x_right << endl;
cout << "dim_x_top"<<dim_y_top << endl;
cout << "dim_y_bot"<<dim_y_bot << endl;
cout << "ENTER M_CELLS" << endl;
cout << it.curr->m_cells[0][0] << endl;
//now that you've found the earliest node that contains something
for (int j = QTQ_GRID_DIM - 1; j >= 0; j--) //y-axis
{
for (int i = 0; i < QTQ_GRID_DIM - 1; i++) //x-axis
{
//cout << "CHIMP" << endl;
//cout << it.curr->m_cells[i][j] << endl;
//cout << "LOOKING" << endl;
//cout << "NUMBER CONTAINED:"<< it.curr->m_cells[i][j] << endl;
if (it.curr->m_cells[i][j] != 0)
{
cout << "FOUND IT!!!" << endl;
it.currCell = *(it.curr->m_cells + (j*QTQ_GRID_DIM) + i);
it.x = dim_x_left + i;
it.y = dim_y_bot + j;
}
}
}
//cout << it.curr->location << endl;
return it;
}
QuadTree::iterator QuadTree::end() //same as begin(), except in opposite order
{
cout << "Iterator END Running" << endl;
iterator it = iterator();
cout << "After iterator constructor!" << endl;
it.curr = QuadTree::m_root; //start with root
int dim_x_left = m_qBounds.m_bL.m_x; //initial dim_x_left is equal to the left wall
int dim_x_right = m_qBounds.m_bL.m_x + m_qBounds.m_dim; // ditto right wall
int dim_y_top = m_qBounds.m_bL.m_y + m_qBounds.m_dim; //ditto top wall
int dim_y_bot = m_qBounds.m_bL.m_y; //ditto bottom wall
cout << "END: Before While" << endl;
while (it.curr->m_quads[0] != NULL || it.curr->m_quads[1] != NULL || it.curr->m_quads[2] != NULL || it.curr->m_quads[3] != NULL
|| it.curr->parent != NULL) //keep going unless all children are null or parent is null
{
cout << "CHIMP" << endl;
if (it.curr->m_quads[3] != NULL) //if first is not null, send pointer there
{
dim_x_right = dim_x_right / 2; //update wall
dim_y_top = dim_y_top / 2; //update top wall
it.curr = it.curr->m_quads[3]; //send pointer there
it.curr->location = 3; //QTQuad Locator
}
else if (it.curr->m_quads[2] != NULL) //and so on
{
dim_x_left = dim_x_right / 2; //update left wall
dim_y_top = dim_y_top / 2; //update top wall
it.curr = it.curr->m_quads[2]; //send pointer there
it.curr->location = 2; //location
}
else if (it.curr->m_quads[1] != NULL)
{
dim_x_left = dim_x_right / 2;
dim_y_bot = dim_y_top / 2;
it.curr = it.curr->m_quads[1];
it.curr->location = 1;
}
else if (it.curr->m_quads[0] != NULL)
{
dim_x_right = dim_x_right / 2;
dim_y_bot = dim_y_top / 2;
it.curr = it.curr->m_quads[0];
it.curr->location = 0;
}
}
cout << "END:AFTER WHILE" << endl;
if (it.curr->parent == NULL)
{
it.curr = m_root;
it.currCell = 0;
//cout << "Raptors!" << endl;
}
//now that you've found the earliest node that contains something
for (int j = 0; j <= QTQ_GRID_DIM - 1; j++) //y-axis
{
//cout << "CHIMP" << endl;
for (int i = QTQ_GRID_DIM - 1; i >= 0; i--) //x-axis
{
//cout << "Chuck Norris" << endl;
if (it.curr->m_cells[i][j] != 0)
{
it.currCell = *(it.curr->m_cells + (j*QTQ_GRID_DIM) + i);
it.x = dim_x_left + i;
it.y = dim_y_bot + j;
}
}
}
//cout << "raptors!" << endl;
cout << it.curr->location << "END: BEFORE RETURN"<< endl;
return it;
}
const QuadTree::iterator & QuadTree::iterator::operator=(const iterator &rhs)
{
//cout << "OPERATOR RUNNING" << endl;
curr = rhs.curr;
currCell = rhs.currCell;
root = rhs.root;
x = rhs.x;
y = rhs.y;
//cout << "OPERATOR RUNNING" << endl;
return rhs;
}
bool QuadTree::iterator::operator==(const QuadTree::iterator &other)
{
return(curr == other.curr && currCell == other.currCell);
}
bool QuadTree::iterator::operator!=(const QuadTree::iterator &other)
{
return(curr != other.curr || currCell != other.currCell);
}
QuadTree::iterator QuadTree::iterator::operator++(int dummy) // Postfix: "it++"
{
QuadTree::iterator result = iterator::iterator();
//result.curr = curr;
//get to the next possible location to start with
int location = curr->location;
if (location < 3)
{
curr = curr->m_quads[location + 1];
location = location + 1;
}
else
{
curr = curr->parent;
}
//while curr is null, keep looking
while (curr == NULL)
{
location = curr->location;
if (location < 3)
{
curr = curr->m_quads[location + 1];
location = location + 1;
}
else
{
curr = curr->parent;
}
}
curr->location = location;
//while currCell is 0, keep looking
while (currCell == NULL)
{
for (int j = QTQ_GRID_DIM; j > 0; j--)
{
for (int i = 0; i < QTQ_GRID_DIM; i++)
{
currCell = currCell + j * QTQ_GRID_DIM + i;
}
}
}
result.curr = curr;
result.currCell = currCell;
result.x = x;
result.y = y;
return result;
}
QuadTree::iterator & QuadTree::iterator::operator++() // Prefix: e.g. "++it"
{
iterator it = iterator();
//QTQuad* temp = curr;
while (curr == NULL)
{
int location = curr->location; //This updates the "location" so that it starts with the correct point
if (location < 3)
{
curr = curr->m_quads[location + 1];
}
else
{
curr = curr->parent;
}
}
while (currCell == NULL)
{
for (int j = QTQ_GRID_DIM; j > 0; j--)
{
for (int i = 0; i < QTQ_GRID_DIM; i++)
{
currCell = currCell + j * QTQ_GRID_DIM + i;
}
}
}
//result.curr = curr;
return it;
}
Point & QuadTree::iterator::operator*()
{
Point resultPoint = Point::Point(x, y);
return resultPoint;
}
int QuadTree::get(const Point &pt)
{
cout << "GET FUNCTION RUNNING" << endl;
int data = 0;
int save_x = 0;
int save_y = 0;
//cout << "Wedge Antilles" << endl;
QTQuad * curr;
curr = m_root;
int dim_x_left = (int)m_qBounds.m_bL.m_x;
int dim_x_right = (int)m_qBounds.m_bL.m_x + (int)m_qBounds.m_dim;
int dim_y_bot = (int)m_qBounds.m_bL.m_y;
int dim_y_top = (int)m_qBounds.m_bL.m_y + (int)m_qBounds.m_dim;
cout << "dim_x_left" << dim_x_left << endl;
cout << "dim_x_right" << dim_x_right << endl;
//cout << "Wedge Antilles" << endl;
if (!m_qBounds.inBounds(pt)) //if out of bounds, just return and don't do anything else.
{
return 0;
}
//cout << "Past out of bounds" << endl;
cout << "Past our of bounds" << endl;
cout << dim_x_right - dim_x_left << endl;
while (dim_x_right - dim_x_left > QTQ_GRID_DIM) //narrow down to QTQuad
{
cout << "inside while loop" << endl;
cout << dim_x_right - dim_x_left << endl;
if (pt.m_x < (dim_x_left + dim_x_right) / 2) //narrow down left and right side.
{
dim_x_right = (dim_x_right + dim_x_left) / 2;
cout << "dim_x_right" << dim_x_right << endl;
if (pt.m_y < (dim_y_top + dim_y_bot) / 2) //narrow down top and bottom sides
{
//cout << "Wedge" << endl;
if (curr->m_quads[0] == NULL)
{
curr->m_quads[0] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[0];
dim_y_top = (dim_y_top + dim_y_bot) / 2;
}
else
{
if (curr->m_quads[2] == NULL)
{
curr->m_quads[2] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[2];
dim_y_bot = (dim_y_top + dim_y_bot) / 2;
}
}
else if (pt.m_x > (dim_x_right + dim_x_left) / 2)
{
dim_x_left = (dim_x_right + dim_x_left) / 2;
cout << "dim_x_left" << dim_x_left << endl;
if (pt.m_y < (dim_y_top + dim_y_bot) / 2)
{
if (curr->m_quads[1] == NULL)
{
curr->m_quads[1] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[1];
dim_y_top = (dim_y_top + dim_y_bot) / 2;
cout << "dim_y_top" << dim_y_top << endl;
}
else
{
if (curr->m_quads[3] == NULL)
{
curr->m_quads[3] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[3];
dim_y_bot = (dim_y_top + dim_y_bot) / 2;
cout << "dim_y_bot" << dim_y_bot << endl;
}
}
else
{
return 0;
}
}
cout << "NODE FOUND" << endl;
cout << "dim_y_bot" << dim_y_bot << endl;
cout << "dim_x_left" << dim_x_left << endl;
//Now that we have the bottom and left sides, we can search through the m_cells
for (int j = QTQ_GRID_DIM; j >= 0; j--) //y value
{
for (int i = 0; i < QTQ_GRID_DIM; i++) //x value
{
if (dim_x_left + i == pt.m_x && dim_y_bot + j == pt.m_y) //leftWall+i, bottomWall+j equal to the point
{
cout << "Cell Found" << endl;
cout << pt.m_x << " " << dim_x_left + i << endl;
cout << pt.m_y << dim_y_bot + j << endl;
//cout << "DATA SET" << endl;
//cout << "Skywalker" << endl;
data=curr->m_cells[i][j] ; //set the data
cout << "DATA SET" << endl;
cout << data << endl;
//currCell = *(curr->m_cells + (j*QTQ_GRID_DIM) + i);//the currCell iterator value is set
//x = pt.m_x;
//y = pt.m_y;
save_x = pt.m_x;
save_y = pt.m_y;
}
}
}
cout << data << endl;
return data;
}
void QuadTree::set(const Point &pt, int data)
{
int save_x=0;
int save_y=0;
QTQuad * curr;
curr = m_root;
int dim_x_left = (int)m_qBounds.m_bL.m_x;
int dim_x_right = (int)m_qBounds.m_bL.m_x + (int)m_qBounds.m_dim;
int dim_y_bot = (int)m_qBounds.m_bL.m_y;
int dim_y_top = (int)m_qBounds.m_bL.m_y + (int)m_qBounds.m_dim;
if (!m_qBounds.inBounds(pt)) //if out of bounds, just return and don't do anything else.
{
return;
}
while (dim_x_right - dim_x_left > QTQ_GRID_DIM) //narrow down to QTQuad
{
//cout << "inside while loop" << endl;
if (pt.m_x < (dim_x_left + dim_x_right) / 2) //narrow down left and right side.
{
dim_x_right = (dim_x_right + dim_x_left) / 2;
//cout << "dim_x_right"<<dim_x_right << endl;
if (pt.m_y < (dim_y_top + dim_y_bot) / 2) //narrow down top and bottom sides
{
if (curr->m_quads[0] == NULL)
{
curr->m_quads[0] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[0];
dim_y_top = (dim_y_top + dim_y_bot) / 2;
}
else
{
if (curr->m_quads[2] == NULL)
{
curr->m_quads[2] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[2];
dim_y_bot = (dim_y_top + dim_y_bot) / 2;
}
}
else //if (pt.m_x > (dim_x_right + dim_x_left) / 2)
{
dim_x_left = (dim_x_right + dim_x_left) / 2;
//cout << "dim_x_left" << dim_x_left << endl;
if (pt.m_y < (dim_y_top + dim_y_bot) / 2)
{
if (curr->m_quads[1] == NULL)
{
curr->m_quads[1] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[1];
dim_y_top = (dim_y_top + dim_y_bot) / 2;
}
else
{
if (curr->m_quads[3] == NULL)
{
curr->m_quads[3] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[3];
dim_y_bot = (dim_y_top + dim_y_bot) / 2;
}
}
}
//cout << "NODE FOUND" << endl;
//cout << "dim_y_bot"<<dim_y_bot << endl;
//cout << "dim_x_left" << dim_x_left << endl;
/*
Now that we have the bottom and left sides, we can search through the m_cells
*/
cout << "Point" << pt.m_x << "," << pt.m_y << endl;
for (int j = QTQ_GRID_DIM; j >= 0; j--) //y value
{
for (int i = 0; i < QTQ_GRID_DIM; i++) //x value
{
if (dim_x_left + i == pt.m_x && dim_y_bot + j == pt.m_y) //leftWall+i, bottomWall+j equal to the point
{
cout << "CELL FOUND" << endl;
//cout << "Cell Found" << endl;
//cout << pt.m_x << " " << dim_x_left+i << endl;
//cout << pt.m_y << dim_y_bot + j << endl;
//cout << "Skywalker" << endl;
curr->m_cells[i][j] = data; //set the data
save_x = pt.m_x;
save_y = pt.m_y;
}
}
}
cout << "SET FUNCTION" << endl;
//cout << curr->m_cells[pt.m_x][pt.m_y] << endl;
cout << "SET FUNCTION BEFORE IF" << endl;
if (curr->m_cells[pt.m_x][pt.m_y] == 0) //IF the current value is set to zero, we have more stuff to do.
{
cout << "REFACTOR" << endl;
for (int i = 0; i < QTQ_GRID_DIM; i++) //scan through x-values
{
for (int j = 0; j < QTQ_GRID_DIM; j++) //scan through y-values
{
if (curr->m_cells[i][j] == 0) //if you find a zero value
{
//it.curr = it.curr->parent;
//it.curr->location = 0;
//currCell = *(curr->m_cells + j * QTQ_GRID_DIM + i);
}
else //if you find a non-zero vlaue, then your work is done.
{
return;
}
}
}
curr = curr->parent; //you didn't find a valued cell, so you need to jump to the parent.
curr->location = 0;
cout << "BEFORE WHILE LOOP" << endl;
/* Keep going if there are any non-null children AND there is a parent to the current cell*/
while ((curr->m_quads[0] != NULL || curr->m_quads[1] != NULL || curr->m_quads[2] != NULL || curr->m_quads[3] != NULL) ||
curr->parent != NULL)
{
cout << "INSIDE WHILE LOOP" << endl;
if (curr->m_quads[0] != NULL) //if the first child is not null, update the current pointer
{
curr = curr->m_quads[0];
}
else if (curr->m_quads[1] != NULL)
{
curr = curr->m_quads[1];
}
else if (curr->m_quads[2] != NULL)
{
curr = curr->m_quads[2];
}
else if (curr->m_quads[3] != NULL)
{
curr = curr->m_quads[3];
}
else //if there are no non-null children, then update the current pointer to be the parent.
{
curr = curr->parent;
curr->m_quads[curr->location] = NULL; //delete the node departed from
curr->location = 0; //re-update location
}
}
}
//cout << "Skywalker" << endl;
cout << "END OF SET FUNCTION" << endl;
return;
}
int QuadTree::increment(const Point &pt, int delta)
{
int returnValue = 0;
int save_x = 0;
int save_y = 0;
//cout << "Wedge Antilles" << endl;
QTQuad * curr;
int * currCell;
curr = m_root;
int dim_x_left = (int)m_qBounds.m_bL.m_x;
int dim_x_right = (int)m_qBounds.m_bL.m_x + (int)m_qBounds.m_dim;
int dim_y_bot = (int)m_qBounds.m_bL.m_y;
int dim_y_top = (int)m_qBounds.m_bL.m_y + (int)m_qBounds.m_dim;
cout << "dim_x_left" << dim_x_left << endl;
cout << "dim_x_right" << dim_x_right << endl;
//cout << "Wedge Antilles" << endl;
if (!m_qBounds.inBounds(pt)) //if out of bounds, just return and don't do anything else.
{
return returnValue;
}
cout << pt.m_x << endl;
cout << dim_x_right - dim_x_left << endl;
while (abs(dim_x_right - dim_x_left) > QTQ_GRID_DIM) //narrow down to QTQuad
{
cout << "inside while loop" << endl;
if (pt.m_x < (dim_x_left + dim_x_right) / 2) //narrow down left and right side.
{
dim_x_right = (dim_x_right + dim_x_left) / 2;
cout << "dim_x_right" << dim_x_right << endl;
if (pt.m_y < (dim_y_top + dim_y_bot) / 2) //narrow down top and bottom sides
{
//cout << "Wedge" << endl;
if (curr->m_quads[0] == NULL)
{
curr->m_quads[0] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[0];
dim_y_top = (dim_y_top + dim_y_bot) / 2;
}
else
{
if (curr->m_quads[2] == NULL)
{
curr->m_quads[2] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[2];
dim_y_bot = (dim_y_top + dim_y_bot) / 2;
}
}
else if (pt.m_x > (dim_x_right + dim_x_left) / 2)
{
dim_x_left = (dim_x_right + dim_x_left) / 2;
cout << "dim_x_left" << dim_x_left << endl;
if (pt.m_y < (dim_y_top + dim_y_bot) / 2)
{
if (curr->m_quads[1] == NULL)
{
curr->m_quads[1] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[1];
dim_y_top = (dim_y_top + dim_y_bot) / 2;
cout << "dim_y_top" << dim_y_top << endl;
}
else
{
if (curr->m_quads[3] == NULL)
{
curr->m_quads[3] = new QTQuad(); //CHECK with TA!!!
}
curr = curr->m_quads[3];
dim_y_bot = (dim_y_top + dim_y_bot) / 2;
cout << "dim_y_bot" << dim_y_bot << endl;
}
}
}
cout << "NODE FOUND" << endl;
cout << "dim_y_bot" << dim_y_bot << endl;
cout << "dim_x_left" << dim_x_left << endl;
/*
Now that we have the bottom and left sides, we can search through the m_cells
*/
for (int j = QTQ_GRID_DIM; j >= 0; j--) //y value
{
for (int i = 0; i < QTQ_GRID_DIM; i++) //x value
{
if (dim_x_left + i == pt.m_x && dim_y_bot + j == pt.m_y) //leftWall+i, bottomWall+j equal to the point
{
cout << "Cell Found" << endl;
cout << pt.m_x << " " << dim_x_left + i << endl;
cout << pt.m_y << dim_y_bot + j << endl;
//cout << "Skywalker" << endl;
curr->m_cells[i][j] = curr->m_cells[i][j]+delta; //set the data
returnValue = curr->m_cells[i][j];
cout << endl << endl << "RETURN VALUE" << returnValue << endl << endl;
currCell = *(curr->m_cells + (j*QTQ_GRID_DIM) + i);//the currCell iterator value is set
//x = pt.m_x;
//y = pt.m_y;
save_x = pt.m_x;
save_y = pt.m_y;
}
}
}
//cout << "Wedge" << endl;
//cout << save_x << endl;
//cout << save_y << endl;
if (curr->m_cells[save_x][save_y] == 0) //IF the current value is set to zero, we have more stuff to do.
{
//cout << "REFACTOR" << endl;
for (int i = 0; i < QTQ_GRID_DIM; i++) //scan through x-values
{
for (int j = 0; j < QTQ_GRID_DIM; j++) //scan through y-values
{
if (curr->m_cells[i][j] == 0) //if you find a zero value, just update the currCell pointer
{
//it.curr = it.curr->parent;
//it.curr->location = 0;
currCell = *(curr->m_cells + j * QTQ_GRID_DIM + i);
}
else //if you find a non-zero vlaue, then your work is done.
{
currCell = *(curr->m_cells + j * QTQ_GRID_DIM + i);
return returnValue;
}
}
}
curr = curr->parent; //you didn't find a valued cell, so you need to jump to the parent.
curr->location = 0;
/* Keep going if there are any non-null children AND there is a parent to the current cell*/
while ((curr->m_quads[0] != NULL || curr->m_quads[1] != NULL || curr->m_quads[2] != NULL || curr->m_quads[3] != NULL) ||
curr->parent != NULL)
{
if (curr->m_quads[0] != NULL) //if the first child is not null, update the current pointer
{
curr = curr->m_quads[0];
}
else if (curr->m_quads[1] != NULL)
{
curr = curr->m_quads[1];
}
else if (curr->m_quads[2] != NULL)
{
curr = curr->m_quads[2];
}
else if (curr->m_quads[3] != NULL)
{
curr = curr->m_quads[3];
}
else //if there are no non-null children, then update the current pointer to be the parent.
{
curr = curr->parent;
curr->m_quads[curr->location] = NULL; //delete the node departed from
curr->location = 0; //re-update location
}
}
}
//cout << "Skywalker" << endl;
return returnValue;
}
void QuadTree::clearAll()
{
iterator it = begin();
while (m_root->m_quads[0] != NULL || m_root->m_quads[1] != NULL || m_root->m_quads[2] != NULL || m_root->m_quads[3] != NULL)
{
//it++;
int x = it.curr->x;
int y = it.curr->y;
Point deleteptr = Point(x, y);
set(deleteptr, 0);
}
it.curr = m_root;
}
QuadTree::~QuadTree()
{
clearAll();
delete m_root;
m_root = NULL;
}
void QuadTree::dump() {
cout << "============================================================\n"
<< "START Dump of quadtree: true bounds = " << m_bounds << "\n"
<< "------------------------------------------------------------"
<< endl;
m_root->dump(m_qBounds);
cout << "------------------------------------------------------------\n"
<< "END Dump of quadtree\n"
<< "============================================================"
<< endl;
}