forked from seub/CirclePackings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_colorer.cpp
355 lines (307 loc) · 8.02 KB
/
graph_colorer.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*This file is part of Circle Packings.
Circle Packings is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Circle Packings is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Circle Packings. If not, see <http://www.gnu.org/licenses/>.*/
#include <QColor>
#include "graph_colorer.hpp"
Graph_Colorer::Graph_Colorer(Graph<Circle> *graph, const coloring_type &coloring)
{
graph_ = graph;
coloring_ = coloring;
}
void Graph_Colorer::color()
{
switch(coloring_)
{
case PLAIN:
color_plain();
break;
case STRIPES1:
color_stripes1();
break;
case STRIPES2:
color_stripes2();
break;
case STRIPES3:
color_stripes3();
break;
case FLOWERS:
color_flowers();
break;
case TILING:
color_tiling();
break;
case SHADING:
color_shading();
break;
case ERDOS:
color_erdos();
break;
default:
std::cout << "ERROR in Graph_Colorer::color: flag problem" << std::endl;
throw(QString("ERROR in Graph_Colorer::color: flag problem"));
}
return;
}
void Graph_Colorer::color_plain()
{
QColor color(100, 215, 255);
unsigned int i;
for(i=0; i<graph_->nb_vertices(); i++)
{
graph_->set_color_by_index(i, color);
}
return;
}
void Graph_Colorer::color_stripes1()
{
QColor color1(138, 43, 226);
QColor color2(51, 204, 255);
QColor color;
int y;
unsigned int i;
for(i=0; i<graph_->nb_vertices(); i++)
{
y = graph_->get_tiling_coordinates_by_index(i).second;
if (y%2==0)
{
color = color1;
}
else
{
color = color2;
}
graph_->set_color_by_index(i, color);
}
return;
}
void Graph_Colorer::color_stripes2()
{
int x,y;
QColor color1(255, 130, 0);
QColor color2(255, 210, 10);
QColor color;
unsigned int i;
for(i=0; i<graph_->nb_vertices(); i++)
{
x = graph_->get_tiling_coordinates_by_index(i).first;
y = graph_->get_tiling_coordinates_by_index(i).second;
if ((x+(y+1)/2)%2==0)
{
color = color1;
}
else
{
color = color2;
}
graph_->set_color_by_index(i, color);
}
return;
}
void Graph_Colorer::color_stripes3()
{
int x,y;
QColor color1(55, 215, 55);
QColor color2(30, 130, 30);
QColor color;
unsigned int i;
for(i=0; i<graph_->nb_vertices(); i++)
{
x = graph_->get_tiling_coordinates_by_index(i).first;
y = graph_->get_tiling_coordinates_by_index(i).second;
if ((x+y/2)%2==0)
{
color = color1;
}
else
{
color = color2;
}
graph_->set_color_by_index(i, color);
}
return;
}
void Graph_Colorer::color_flowers()
{
int x,y;
QColor color_2 = QColor(255, 160, 20);
QColor color_1 = QColor(160, 0, 220);
QColor color;
unsigned int i;
for(i=0;i<graph_->nb_vertices();i++)
{
x = graph_->get_tiling_coordinates_by_index(i).first;
y = graph_->get_tiling_coordinates_by_index(i).second;
if (y%2==1)
{
color = color_1;
}
else
{
color = (x+y/2)%2==0 ? color_1 : color_2;
}
graph_->set_color_by_index(i, color);
}
return;
}
void Graph_Colorer::color_tiling()
{
QColor color1(255,204,0);
QColor color2(51,204,0);
QColor color3(51,102,255);
QColor color;
unsigned int x,y,t;
unsigned int i;
for(i=0; i<graph_->nb_vertices(); i++)
{
x = graph_->get_tiling_coordinates_by_index(i).first;
y = graph_->get_tiling_coordinates_by_index(i).second;
t = (x + y%2)%3;
if (t==0)
{
color = color1;
}
else if (t==1)
{
color = color2;
}
else if (t==2)
{
color = color3;
}
graph_->set_color_by_index(i, color);
}
return;
}
void Graph_Colorer::extrema_tiling(int &x_min, int &x_max, int &y_min, int &y_max)
{
integer_coordinates xy = graph_->get_tiling_coordinates_by_index(0);
x_min = xy.first;
x_max = xy.first;
y_min = xy.second;
y_max = xy.second;
unsigned int i;
for(i=1; i<graph_->nb_vertices(); i++)
{
xy = graph_->get_tiling_coordinates_by_index(i);
if (xy.first < x_min)
{
x_min = xy.first;
}
if (xy.first > x_max)
{
x_max = xy.first;
}
if (xy.second < y_min)
{
y_min = xy.second;
}
if (xy.second > y_max)
{
y_max = xy.second;
}
}
return;
}
void Graph_Colorer::color_shading()
{
int x_min, x_max, y_min, y_max, x, y;
extrema_tiling(x_min, x_max, y_min, y_max);
int color1[3]={255,255,255};
int color2[3]={255,0,0};
int color3[3]={255,255,0};
int color4[3]={0,0,255};
int rouge, vert, bleu;
double t1,t2;
unsigned int i;
for(i=0; i<graph_->nb_vertices(); i++)
{
x = graph_->get_tiling_coordinates_by_index(i).first;
y = graph_->get_tiling_coordinates_by_index(i).second;
t1=(x-x_min)*1.0/(x_max-x_min);
t2=(y-y_min)*1.0/(y_max-y_min);
rouge=(int) (t1*color1[0]+(1-t1)*color2[0]+t2*color3[0]+(1-t2)*color4[0])/2;
vert=(int) ((t1*color1[1]+(1-t1)*color2[1]+t2*color3[1]+(1-t2)*color4[1])/2);
bleu=(int) ((t1*color1[2]+(1-t1)*color2[2]+t2*color3[2]+(1-t2)*color4[2])/2);
rouge = std::min(std::max(rouge, 0),255);
vert = std::min(std::max(vert, 0),255);
bleu = std::min(std::max(bleu, 0),255);
graph_->set_color_by_index(i, QColor(rouge,vert,bleu));
}
return;
}
void Graph_Colorer::compute_erdos()
{
unsigned int n = graph_->nb_vertices();
nb_erdos_.resize(n);
std::vector<bool> computed;
computed.resize(n);
fill(computed.begin(),computed.end(),false);
std::vector<unsigned int> previously_computed;
std::vector<unsigned int> new_computed;
unsigned int nb_computed=0;
int nb_erdos = 0;
unsigned int k;
for(k=0; k<graph_->nb_vertices(); k++)
{
if (graph_->get_neighbors_by_index(k).size() < 6)
{
nb_erdos_[k] = nb_erdos + 1;
computed[k] = true;
previously_computed.push_back(k);
nb_computed++;
}
}
nb_erdos++;
std::vector<vertex_label> neighbors;
unsigned int i,j;
while(nb_computed<n)
{
new_computed.clear();
for(i=0; i<previously_computed.size(); i++)
{
neighbors = graph_->get_neighbors_by_index(previously_computed[i]);
for (j=0; j<neighbors.size(); j++)
{
if(!computed[neighbors[j]-1])
{
nb_erdos_[neighbors[j]-1] = nb_erdos+1;
computed[neighbors[j]-1] = true;
new_computed.push_back(neighbors[j]-1);
nb_computed++;
}
}
}
previously_computed = new_computed;
nb_erdos++;
}
return;
}
void Graph_Colorer::color_erdos()
{
compute_erdos();
QColor color_1 = QColor(100, 149, 237);
QColor color_2 = QColor(25, 25, 112);
QColor color;
unsigned int i;
for(i=0; i<graph_->nb_vertices(); i++)
{
if ((nb_erdos_[i]/2)%2 == 0)
{
color = color_1;
}
else
{
color = color_2;
}
graph_->set_color_by_index(i, color);
}
return;
}