-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacking.cpp
More file actions
96 lines (84 loc) · 2.56 KB
/
Copy pathPacking.cpp
File metadata and controls
96 lines (84 loc) · 2.56 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
#include "Packing.h"
RP_Node::RP_Node()
{
this->image = NULL;
this->left_child = NULL;
this->right_child = NULL;
}
RP_Node* RP_Node::Insert(CSourceImage* img)
{
if(this->left_child!=NULL && this->right_child!=NULL)
{
RP_Node* newNode = this->left_child->Insert(img);
if(newNode!=NULL)
return newNode;
return this->right_child->Insert(img);
}
else
{
if(this->image!=NULL)
return NULL;
//too small?
if( (img->GetHeight() > this->height) || (img->GetWidth() > this->width))
return NULL;
//accept?
if( (img->GetHeight() == this->height) && (img->GetWidth() == this->width))
{
this->image = img;
return this;
}
//split
this->left_child = new RP_Node();
this->right_child = new RP_Node();
int dw = this->width - img->GetWidth();
int dh = this->height - img->GetHeight();
if(dw > dh)
{
this->left_child->left = this->left;
this->left_child->top = this->top;
this->left_child->width = img->GetWidth();
this->left_child->height = this->height;
this->right_child->left = this->left + img->GetWidth();
this->right_child->top = this->top;
this->right_child->width = dw;
this->right_child->height = this->height;
}
else
{
this->left_child->left = this->left;
this->left_child->top = this->top;
this->left_child->width = this->width;
this->left_child->height = img->GetHeight();
this->right_child->left = this->left;
this->right_child->top = this->top + img->GetHeight();
this->right_child->width = this->width;
this->right_child->height = dh;
}
return left_child->Insert(img);
}
}
bool RP_Node::Unwind()
{
if(this->left_child == NULL || this->right_child == NULL)
{
if(this->image != NULL)
{
this->image->SetLeft(this->left);
this->image->SetTop(this->top);
}
return true;
}
else
{
if(this->left_child->Unwind())
delete left_child;
if(this->right_child->Unwind())
delete right_child;
if(this->image != NULL)
{
this->image->SetLeft(this->left);
this->image->SetTop(this->top);
}
return true;
}
}