-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixel.cpp
203 lines (161 loc) · 3.9 KB
/
Pixel.cpp
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
#include "Pixel.h"
//Vector methods
void Vector2::initialize(){
x = -1;
y = -1;
}
bool Vector2::operator==(const Vector2& rhs){
return (this->x == rhs.x && this->y == rhs.y);
}
bool Vector2::operator!=(const Vector2& rhs){
return (!(*this == rhs));
}
Pixel::Pixel(bool _on){
on = _on;
isSquare = false;
sqRadius = 0;
neighbors = NULL;
scanned = false;
//position.initialize();
}
Pixel::Pixel(int r, int c, bool _on){
on = _on;
isSquare = false;
sqRadius = 0;
position.x = r;
position.y = c;
neighbors = NULL;
scanned = false;
}
void Pixel::turnOn(bool _on){
on = _on;
}
bool Pixel::isOn(){
return on;
}
bool Pixel::isPainted(){
return painted;
}
void Pixel::setPainted(bool _paint){
painted = _paint;
}
Vector2 Pixel::getPosition(){
return position;
}
void Pixel::setNeighbours(Pixel** pixelGrid, int _R, int _C, list<Pixel> & squarePixels){
//returns the pixel surronding a cell to make a square
bool stop = !this->isOn();
int radius = 1;
//Loop if the Pixel is 'on'
while (!stop){
vector<Pixel> temp;
for (int r = -radius; r <= radius; r++){
for (int c = -radius; c <= radius; c++){
Vector2 a;
a.x = this->position.x + r;
a.y = this->position.y + c;
if ((a.x < _R && a.x >= 0) && (a.y < _C && a.y >= 0)){
Pixel p = pixelGrid[a.x][a.y];
if (p.on && !p.painted){
temp.push_back(p);
}
else
break;
}
}
}
if (temp.size() == (2 * radius + 1) * (2 * radius + 1)){ //ie. we've found a square
neighbors = new Pixel[(2 * radius + 1) * (2 * radius + 1)];
int i = 0;
for (Pixel p : temp){
neighbors[i] = p;
i++;
}
//This pixel is a center of a square
this->isSquare = true;
this->sqRadius = radius;
squarePixels.push_back(*this);
//printf("Cell pos: (%d, %d) Radius: %d\n", this->position.x, this->position.y, this->sqRadius);
radius++;
}
else{
this->sqRadius = 0;
stop = true;
}
}
}
void Pixel::setIncompleteSquareNeighbours(Pixel** pixelGrid, int _R, int _C, list<Pixel> & squarePixels, vector<Pixel> & toBeErased){
//returns the pixel surronding a cell to make a square
bool stop = !this->isOn();
int radius = 1;
int erasableCount = 0;
int tolerance = 1;
//Loop if the Pixel is 'on'
while (!stop){
vector<Pixel> temp;
vector<Pixel> erasable;
for (int r = -radius; r <= radius; r++){
for (int c = -radius; c <= radius; c++){
Vector2 a;
a.x = this->position.x + r;
a.y = this->position.y + c;
if ((a.x < _R && a.x >= 0) && (a.y < _C && a.y >= 0)){
Pixel p = pixelGrid[a.x][a.y];
if (p.on && !p.painted){
temp.push_back(p);
}
else{
if ((erasableCount < tolerance) && !p.scanned){
erasableCount++;
erasable.push_back(p);
temp.push_back(p);
pixelGrid[p.getPosition().x][p.getPosition().y].scanned = true;
}
else{
erasable.clear();
break;
}
}
}
}
}
if (temp.size() == (2 * radius + 1) * (2 * radius + 1)){ //ie. we've found a square
neighbors = new Pixel[(2 * radius + 1) * (2 * radius + 1)];
int i = 0;
for (Pixel p : temp){
neighbors[i] = p;
i++;
}
for (Pixel p : erasable){
toBeErased.push_back(p);
}
//This pixel is a center of a square
this->isSquare = true;
this->sqRadius = radius;
squarePixels.push_back(*this);
printf("Cell pos: (%d, %d) Radius: %d\n", this->position.x, this->position.y, this->sqRadius);
radius++;
tolerance = radius + 1;
}
else{
this->sqRadius = 0;
stop = true;
}
}
}
void Pixel::clearNeighbors(){
delete[] neighbors;
}
void Pixel::setOn(bool _on){
on = _on;
}
/* List methods, inverted so that the largest values come on top*/
bool Pixel::operator<(const Pixel& rhs){
return (this->sqRadius > rhs.sqRadius);
}
bool Pixel::operator==(const Pixel& rhs){
return (this->sqRadius == rhs.sqRadius);
}
bool Pixel::operator>(const Pixel& rhs){
return (this->sqRadius < rhs.sqRadius);
}