/
TreeNode.cs
117 lines (105 loc) · 3.4 KB
/
TreeNode.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace ZuneGame1
{
class TreeNode
{
Circle item;
TreeNode[] children;
TreeNode parent;
List<Circle> updateList;
List<Circle> drawList;
bool ready;
public TreeNode(Vector2 locXY, List<Circle> update, List<Circle>draw)
{
updateList = update;
drawList = draw;
Main.nodeCount++;
item = new Circle( this, Main.getRandomColour(), locXY, 240, 0);
updateList.Add(item);
drawList.Add(item);
ready = true;
}
public TreeNode(TreeNode par, List<Circle> update, List<Circle> draw, int no)
{
parent = par;
updateList = update;
drawList = draw;
Main.nodeCount++;
item = new Circle(this, parent.item.getColour(), parent.item.getLocXY(), parent.item.getDia(), no);
updateList.Add(item);
drawList.Add(item);
ready = false;
}
public bool inCircle(Vector2 locXY)
{
float rad = item.getDia()/2;
locXY = item.getLocXY() - locXY + new Vector2(rad,rad);
if (locXY.Length() <= rad)
return true;
return false;
}
public TreeNode search(Vector2 cmpLoc)
{
Vector2 locXY = item.getLocXY();
locXY.X += item.getDia() / 2;
locXY.Y += item.getDia() / 2;
//check done
if (cmpLoc.Equals(locXY) || children == null)
return this;
//figure out which child to use
else if (cmpLoc.X > locXY.X)
{
if (cmpLoc.Y > locXY.Y)
{
//search through the child, and its children
return children[4].search(cmpLoc);
}
else
return children[2].search(cmpLoc);
}
else
{
if (cmpLoc.Y > locXY.Y)
{
return children[3].search(cmpLoc);
}
else
return children[1].search(cmpLoc);
}
}
public void split()
{
if (children == null && item.getDia() > 2 && ready && item.getColour() != Color.Black)
{
children = new TreeNode[5];
//children[0] = new TreeNode(new Vector2(item.getLocXY().X + item.getDia() / 2, item.getLocXY().X + item.getDia() / 2), updateList, drawList);
//children[0].item.setDia(item.getDia() / 2);
for (int i = 1; i < 5; i++)
{
children[i] = new TreeNode(this, updateList, drawList, i);
}
drawList.Remove(item);
item.setColour(Color.Black);
Main.nodeCount--;
}
}
public void readyToSplit()
{
ready = true;
}
public void erase()
{
if (children != null && children[0] != null)
{
children[0].item.setColour(Color.White);
drawList.Remove(children[0].item);
children[0] = null;
}
}
}
}