-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFractalTreeImage.cpp
More file actions
171 lines (131 loc) · 5.15 KB
/
FractalTreeImage.cpp
File metadata and controls
171 lines (131 loc) · 5.15 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "FractalTreeImage.h"
#define PI 3.14159265359
FractalTreeImage::FractalTreeImage(int width, int height, int numBranches, int recursionDepth, int rootWidth, float baseSize, float rootStretch, float leafSize, float lowerJitter, float upperJitter, unsigned int seed, QColor treeColor, QColor leafColor) :
QImage(width, height, QImage::Format_ARGB32)
{
if (seed == 0)
this->seed = time(NULL) * (unsigned int)rand();
else
this->seed = seed;
branches = numBranches;
maxDepth = recursionDepth;
this->rootWidth = rootWidth;
this->treeColor = treeColor;
this->leafColors.append(leafColor);
this->leafSize = leafSize;
this->branchStretch = rootStretch;
this->baseSize = baseSize;
this->upperJitter = upperJitter;
this->lowerJitter = lowerJitter;
drawTree();
}
FractalTreeImage::FractalTreeImage(int width, int height, int numBranches, int recursionDepth, int rootWidth, float baseSize, float rootStretch, float leafSize, float lowerJitter, float upperJitter, unsigned int seed, QColor treeColor, QList<QColor> leafColors) :
QImage(width, height, QImage::Format_ARGB32)
{
if (seed == 0)
this->seed = time(NULL) * (unsigned int)rand();
else
this->seed = seed;
branches = numBranches;
maxDepth = recursionDepth;
this->rootWidth = rootWidth;
this->treeColor = treeColor;
this->leafColors.append(leafColors);
this->leafSize = leafSize;
this->branchStretch = rootStretch;
this->baseSize = baseSize;
this->upperJitter = upperJitter;
this->lowerJitter = lowerJitter;
drawTree();
}
void FractalTreeImage::drawTree()
{
srand(seed);
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::HighQualityAntialiasing);
int width = this->width();
int height = this->height();
double baseLength = baseSize * min(width, height) / 3.5;
double delta = 180.0 / (2 * branches);
QList<Endpoint> endpoints;
//remove background
painter.setCompositionMode(QPainter::CompositionMode_Clear);
painter.fillRect(rect(), Qt::transparent);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
//Draw base line
QPoint start(width / 2, height - (height / 10));
QVector2D baseVec(0, -1);
QPoint end = add(start,baseLength * baseVec);
drawLine(start, end, painter, rootWidth);
//insert endpoint of baseline into endpoints list
endpoints.push_back(Endpoint{end, baseVec});
//draw branches
for (double n = 1; n < maxDepth; n++) {
double lineWidth = rootWidth - rootWidth * (n/maxDepth);
lineWidth = max(lineWidth, 1.0);
double length = baseLength / (branchStretch * n + 1.0);
QList<Endpoint> newEndpoints;
for (Endpoint endpoint : endpoints) {
double angle = delta - 90;
for (int i = 0; i < branches; i++) {
double offset = delta / 2 * lowerJitter;
double range = delta / 2 * upperJitter; + offset;
double jitter = drand() * range - offset; //calculate jitter
QPoint start = endpoint.point;
QVector2D direction = rotate(endpoint.vec, angle + jitter);
QPoint end = add(start, length * direction);
drawLine(start, end, painter, lineWidth);
newEndpoints.append(Endpoint{end, direction});
angle += 2 * delta;
}
}
endpoints.clear();
endpoints.append(newEndpoints);
}
//draw leafs
for (Endpoint p : endpoints) {
drawLeaf(p, baseLength * leafSize, painter);
}
painter.end();
}
void FractalTreeImage::drawLine(QPoint start, QPoint end, QPainter &painter, float thickness) {
QPen pen(treeColor, thickness, Qt::DotLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
QVector2D vec(end-start);
QPoint cp = add(start, vec / 2 + rotate(vec, pow(-1, rand()%2) * 90) / 8);
QPainterPath path(start);
path.quadTo(cp, end);
painter.drawPath(path);
}
void FractalTreeImage::drawLeaf(Endpoint point, int length, QPainter &painter) {
double width = length * 0.6;
QColor color = leafColors.size() > 0? leafColors[rand() % leafColors.size()] : QColor(0, 198, 0, 200);
QVector2D vec = rotate(point.vec, rand() % 40 - 20);
QPoint start = point.point;
QPoint end = add(start, length * vec);
QPoint cp1 = add(start, length * vec / 2 + width * rotate(vec, 90));
QPoint cp2 = add(start, length * vec / 2 + width * rotate(vec, -90));
QPainterPath path(start);
path.quadTo(cp1, end);
path.quadTo(cp2, start);
painter.fillPath(path,QBrush(color));
}
QPoint FractalTreeImage::add(QPoint p, QVector2D v) {
return p + v.toPoint();
}
//angle is conterclockwise as usual
QVector2D FractalTreeImage::rotate(QVector2D vec, double angle) {
double a = -angle * PI / 180.0;
float x = vec.x() * cos(a) - vec.y() * sin(a);
float y = vec.x() * sin(a) + vec.y() * cos(a);
return QVector2D(x, y);
}
unsigned int FractalTreeImage::getSeed()
{
return seed;
}
double FractalTreeImage::drand() {
return (double) rand() / (double) RAND_MAX;
}