-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSPTree.cpp
More file actions
227 lines (187 loc) · 4.81 KB
/
BSPTree.cpp
File metadata and controls
227 lines (187 loc) · 4.81 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
#include"BSPTree.h"
BSPTree::BSPTree()
{
root = NULL;
}
BSPTree::~BSPTree()
{
removeSubTree(root);// menghapus seluruh tree dari memori mdengan memanggil prosedur removesubtree()
}
void BSPTree::removeSubTree(node* ptr)
{
//penelusuran secara post order
if (ptr != NULL)
{
if (ptr->left != NULL)
{
//hapus tree dari memori
removeSubTree(ptr->left);
}
if (ptr->right != NULL)
{
//hapus tree dari memori
removeSubTree(ptr->right);
}
delete ptr;
}
}
void BSPTree::addLeaf(rectangle area)
{
root = createLeaf(area);
}
BSPTree::node* BSPTree::createLeaf(rectangle area)
{
node* leaf = new node;
leaf->area = area;
leaf->left = NULL;
leaf->right = NULL;
return leaf;
}
int BSPTree::randomRange(int start, int size_index)
{
return start + (rand() % size_index);
}
void BSPTree::split(int minWidth, int minHeight, int maxWidth, int maxHeight)
{
splitPrivate(root, minWidth, minHeight, maxWidth, maxHeight);
}
void BSPTree::splitPrivate(node* ptr, int minWidth, int minHeight, int maxWidth, int maxHeight)
{
if (ptr->area.width > maxWidth || ptr->area.height > maxHeight)
{
//menentukan dibagi secara horizontal atau vertikal
bool splitHorizontal;
if (ptr->area.height != ptr->area.width)
{
splitHorizontal = (ptr->area.height > ptr->area.width) ? true : false;
}
else
{
splitHorizontal = (randomRange(0, 2)) ? true : false;
}
//menentukan apakah pohon masih bisa dibagi(dipecah)
bool canSplit = (ptr->area.width > minWidth && ptr->area.height > minHeight) ? true : false;
if (splitHorizontal && canSplit) //membagi secara horizontal
{
//membagi panjang dengan ukuran random
int newHeight = minHeight + randomRange(0, ptr->area.height - minHeight);
//randomRange(0, ptr->area.height);
//randomRange(maxHeight, ptr->area.height - maxHeight);
//minHeight + randomRange(0, ptr->area.height-minHeight*2);
//rectangle setelah dibagi
rectangle leftArea(ptr->area.x, ptr->area.y, ptr->area.width, newHeight);
rectangle rightArea(ptr->area.x, ptr->area.y + newHeight, ptr->area.width, ptr->area.height - newHeight);
//membuat daun baru hasil rectangle setelah dibagi
ptr->left = NULL;
ptr->right = NULL;
ptr->left = createLeaf(leftArea);
ptr->right = createLeaf(rightArea);
//bagi lagi daun yang baru dibuat
splitPrivate(ptr->left, minWidth, minHeight, maxWidth, maxHeight);
splitPrivate(ptr->right, minWidth, minHeight, maxWidth, maxHeight);
}
else if (canSplit) //membagi secara vertikal
{
//membagi panjang dengan ukuran random
int newWidth = minWidth + randomRange(0, ptr->area.height - minWidth);
//randomRange(0, ptr->area.width);
//randomRange(maxWidth,ptr->area.width-maxWidth);
//minWidth + randomRange(0, ptr->area.height-minWidth*2);
//rectangle setelah dibagi
rectangle leftArea(ptr->area.x, ptr->area.y, newWidth, ptr->area.height);
rectangle rightArea(ptr->area.x + newWidth, ptr->area.y, ptr->area.width - newWidth, ptr->area.height);
//membuat daun baru hasil rectangle setelah dibagi
ptr->left = NULL;
ptr->right = NULL;
ptr->left = createLeaf(leftArea);
ptr->right = createLeaf(rightArea);
//bagi lagi daun yang baru dibuat
splitPrivate(ptr->left, minWidth, minHeight, maxWidth, maxHeight);
splitPrivate(ptr->right, minWidth, minHeight, maxWidth, maxHeight);
}
}
}
vector<rectangle> BSPTree::getListOrderValue()
{
getListOrder();
return listOrder;
}
void BSPTree::getListOrder()
{
listOrder.clear();
getListOrderPrivate(root);
}
void BSPTree::getListOrderPrivate(node* ptr)
{
if (ptr == NULL)
return;
//dapatkan data parent
listOrder.push_back(ptr->area);
//dapatkan data kiri
getListOrderPrivate(ptr->left);
//dapatkan data kanan
getListOrderPrivate(ptr->right);
}
vector<rectangle> BSPTree::getListLeafValue()
{
getListLeaf();
return listOrder;
}
void BSPTree::getListLeaf()
{
listOrder.clear();
getListLeafPrivate(root);
}
void BSPTree::getListLeafPrivate(node* ptr)
{
if (ptr == NULL)
return;
//jika merupakan daun
if (ptr->left == NULL && ptr->right == NULL)
{
listOrder.push_back(ptr->area);
}
//cek kiri
getListLeafPrivate(ptr->left);
//cek kanan
getListLeafPrivate(ptr->right);
}
void BSPTree::preOrder()
{
preOrderPrivate(root);
cout << endl;
}
void BSPTree::preOrderPrivate(node* ptr)
{
if (ptr == NULL)
return;
//dapatkan data parent
cout << ptr->area.width << "x" << ptr->area.height << " ";
//dapatkan data kiri
preOrderPrivate(ptr->left);
//dapatkan data kanan
preOrderPrivate(ptr->right);
}
void BSPTree::printInOrder()
{
printInOrderPrivate(root);
}
void BSPTree::printInOrderPrivate(node* ptr)
{
if (root != NULL)
{
if (ptr->left != NULL)
{
printInOrderPrivate(ptr->left);
}
cout << ptr->area.color << " " << endl;
if (ptr->right != NULL)
{
printInOrderPrivate(ptr->right);
}
}
else
{
cout << " kosong " << endl;
}
}