-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpWithObjFunctions.cpp
More file actions
157 lines (122 loc) · 3.81 KB
/
OpWithObjFunctions.cpp
File metadata and controls
157 lines (122 loc) · 3.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
#include "Header.h"
long OperationsWithObjects::Area(MBR mbr) {
return (mbr.x2 - mbr.x1) * (mbr.y2 - mbr.y1);
}
void OperationsWithObjects::CalculateMBR(Node* node) {
int maxX, minX, maxY, minY;
maxX = node->childs[0]->covering.x2;
minX = node->childs[0]->covering.x1;
maxY = node->childs[0]->covering.y2;
minY = node->childs[0]->covering.y1;
for (int i = 1; i < node->objCount; i++) {
if (node->childs[i]->covering.x2 > maxX)
maxX = node->childs[i]->covering.x2;
if (node->childs[i]->covering.x1 < minX)
minX = node->childs[i]->covering.x1;
if (node->childs[i]->covering.y2 > maxY)
maxY = node->childs[i]->covering.y2;
if (node->childs[i]->covering.y1 < minY)
minY = node->childs[i]->covering.y1;
}
node->covering.x1 = minX;
node->covering.x2 = maxX;
node->covering.y1 = minY;
node->covering.y2 = maxY;
}
int OperationsWithObjects::FindMaxBottomBorder(Node** nodes, int nodesCount, int axis) {
int maxBottomBorder = 0;
int maxBottomBorderNum;
if (axis == XAXIS) {
maxBottomBorder = nodes[0]->covering.x1;
maxBottomBorderNum = 0;
for (int i = 1; i < nodesCount; i++) {
if (nodes[i]->covering.x1 > maxBottomBorder) {
maxBottomBorder = nodes[i]->covering.x1;
maxBottomBorderNum = i;
}
}
}
else {
maxBottomBorder = nodes[0]->covering.y1;
maxBottomBorderNum = 0;
for (int i = 1; i < nodesCount; i++) {
if (nodes[i]->covering.y1 > maxBottomBorder) {
maxBottomBorder = nodes[i]->covering.y1;
maxBottomBorderNum = i;
}
}
}
return maxBottomBorderNum;
}
Node* OperationsWithObjects::ChooseNodeWithMinMBR(Node* curNode, Node* newNode) {
MBR curDeltaMBR;
long curArea = 0;
long curDeltaArea = 0;
long minDeltaArea = 0;
int childWithMinDeltaArea;
for (int i = 0; i < curNode->objCount; i++) {
//X1
if (newNode->covering.x1 < curNode->childs[i]->covering.x1)
curDeltaMBR.x1 = newNode->covering.x1;
else
curDeltaMBR.x1 = curNode->childs[i]->covering.x1;
//X2
if (newNode->covering.x2 > curNode->childs[i]->covering.x2)
curDeltaMBR.x2 = newNode->covering.x2;
else
curDeltaMBR.x2 = curNode->childs[i]->covering.x2;
//Y1
if (newNode->covering.y1 < curNode->childs[i]->covering.y1)
curDeltaMBR.y1 = newNode->covering.y1;
else
curDeltaMBR.y1 = curNode->childs[i]->covering.y1;
//Y2
if (newNode->covering.y2 > curNode->childs[i]->covering.y2)
curDeltaMBR.y2 = newNode->covering.y2;
else
curDeltaMBR.y2 = curNode->childs[i]->covering.y2;
curArea = Area(curNode->childs[i]->covering);
curDeltaArea = Area(curDeltaMBR) - curArea;
if (minDeltaArea == 0) {
minDeltaArea = curDeltaArea;
childWithMinDeltaArea = i;
}
if (curDeltaArea == 0)
return curNode->childs[i];
else
if (curDeltaArea < minDeltaArea) {
minDeltaArea = curDeltaArea;
childWithMinDeltaArea = i;
}
}
return curNode->childs[childWithMinDeltaArea];
}
bool OperationsWithObjects::IsIntersect(MBR mbr1, MBR mbr2) {
if (Area(mbr2) < Area(mbr1))
swap(mbr1, mbr2);
if (mbr1.x1 >= mbr2.x1 && mbr1.x1 <= mbr2.x2 && mbr1.y1 >= mbr2.y1 && mbr1.y1 <= mbr2.y2) //x1 y1
return true;
if (mbr1.x2 >= mbr2.x1 && mbr1.x2 <= mbr2.x2 && mbr1.y1 >= mbr2.y1 && mbr1.y1 <= mbr2.y2) //x2 y1
return true;
if (mbr1.x1 >= mbr2.x1 && mbr1.x1 <= mbr2.x2 && mbr1.y2 >= mbr2.y1 && mbr1.y2 <= mbr2.y2) //x1 y2
return true;
if (mbr1.x2 >= mbr2.x1 && mbr1.x2 <= mbr2.x2 && mbr1.y2 >= mbr2.y1 && mbr1.y2 <= mbr2.y2) //x2 y2
return true;
return false;
}
bool OperationsWithObjects::IsFullyContains(MBR container, MBR content) {
if (container.x1 <= content.x1 &&
container.x2 >= content.x2 &&
container.y1 <= content.y1 &&
container.y2 >= content.y2)
return true;
return false;
}
bool OperationsWithObjects::IsEqual(MBR mbr1, MBR mbr2) {
if (mbr1.x1 == mbr2.x1 &&
mbr1.x2 == mbr2.x2 &&
mbr1.y1 == mbr2.y1 &&
mbr1.y2 == mbr2.y2)
return true;
return false;
}