-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmutation.cpp
96 lines (89 loc) · 2.86 KB
/
mutation.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
#include "widget.h"
#include "ui_widget.h"
#include "settings.h"
#include <QDebug>
void Widget::tryAddPoly()
{
Poly poly = genPoly();
QImage newGen = generated;
Poly::drawPoly(newGen, poly);
quint64 newFit = computeFitness(newGen);
if (newFit < fitness)
{
// Update data
int polyPos = polys.size();
polys.append(poly);
QImage predrawn = generated;
generated = newGen;
// Optimize
optimizeColors(polyPos, polys, predrawn);
optimizeShape(polyPos, polys, predrawn);
fitness = computeFitness(generated);
// Update GUI
ui->imgBest->setPixmap(QPixmap::fromImage(generated));
ui->polysLabel->setNum(polyPos+1);
updateGuiFitness();
}
}
void Widget::removePoly(QVector<Poly>& newPolys, QImage &target)
{
int w = (int)(((float)width*(float)(FOCUS_RIGHT-FOCUS_LEFT))/100.0);
int h = (int)(((float)height*(float)(FOCUS_BOTTOM-FOCUS_TOP))/100.0);
int x = (width*(float)FOCUS_LEFT/100.0);
int y = (height*(float)FOCUS_TOP/100.0);
int index;
for (int i=0;i<100;i++)
{
index = qrand()%newPolys.size();
if (newPolys[index].hasPointIn(QRect(x,y,w,h)))
break;
}
newPolys.remove(index);
redraw(target, newPolys);
}
void Widget::shapeOptPoly(QVector<Poly>& newPolys)
{
int index = qrand()%newPolys.size();
optimizeShape(index, newPolys);
}
void Widget::reorderPoly(QVector<Poly>& newPolys, QImage &target)
{
int w = (int)(((float)width*(float)(FOCUS_RIGHT-FOCUS_LEFT))/100.0);
int h = (int)(((float)height*(float)(FOCUS_BOTTOM-FOCUS_TOP))/100.0);
int x = (width*(float)FOCUS_LEFT/100.0);
int y = (height*(float)FOCUS_TOP/100.0);
int source;
for (int i=0;i<100;i++)
{
source = qrand()%newPolys.size();
if (newPolys[source].hasPointIn(QRect(x,y,w,h)))
break;
}
int dest = qrand()%newPolys.size();
Poly poly = newPolys.takeAt(source);
newPolys.insert(dest, poly);
redraw(target, newPolys);
optimizeShape(dest, newPolys);
optimizeColors(dest, newPolys);
}
void Widget::movePoint(QVector<Poly>& newPolys, QImage& target)
{
int w = (int)(((float)width*(float)(FOCUS_RIGHT-FOCUS_LEFT))/100.0);
int h = (int)(((float)height*(float)(FOCUS_BOTTOM-FOCUS_TOP))/100.0);
int x = (width*(float)FOCUS_LEFT/100.0);
int y = (height*(float)FOCUS_TOP/100.0);
int source;
for (int i=0;i<100;i++)
{
source = qrand()%newPolys.size();
if (newPolys[source].hasPointIn(QRect(x,y,w,h)))
break;
}
Poly poly = newPolys[source];
int pointi = qrand()%poly.points.size();
poly.points[pointi].setX(poly.points[pointi].x()+qrand()%20-10);
poly.points[pointi].setY(poly.points[pointi].y()+qrand()%20-10);
redraw(target, newPolys);
optimizeShape(source, newPolys);
optimizeColors(source, newPolys);
}