-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderarea.cpp
186 lines (156 loc) · 4.1 KB
/
renderarea.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
#include "renderarea.h"
#include <iostream>
#include <QPainter>
#include <cmath>
#include <QThread>
// Konstruktor
RenderArea::RenderArea(QWidget *parent)
: QWidget(parent)
{
antialiased = false;
zoom = (sqrt(STARTZOOM*8.0)+10.0)*1.0/4.0;
leftWinkel = STARTWINKEL;
maxDepth = STARTDEPTH;
mutation = false;
//QColor
pen = QPen(Qt::red, 1, /*Qt::SolidLine*/Qt::NoPen, Qt::FlatCap, Qt::MiterJoin);
brush = QBrush(Qt::blue, /*Qt::NoBrush*/Qt::SolidPattern);
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
// Minimale größe drawin Widget
QSize RenderArea::minimumSizeHint() const
{
return QSize(200, 100);
}
// Standardgröße drawing Widget
QSize RenderArea::sizeHint() const
{
return QSize(600, 400);
}
// Antialiasing aktivieren
void RenderArea::setAntialiased(bool antialiased)
{
this->antialiased = antialiased;
update();
}
void RenderArea::setMutation(bool mutation)
{
this->mutation = mutation;
update();
}
// Zoom ändern
void RenderArea::setZoom(int zoom)
{
this->zoom = (sqrt(zoom*8.0)+10.0)*1.0/4.0;
update();
}
// Linke Zeichenwinkel ändern
void RenderArea::setLeftWinkel(int leftWinkel)
{
this->leftWinkel = leftWinkel;
update();
}
// Zeichentieve für Baum ändern
void RenderArea::setDepth(int depth)
{
maxDepth = depth;
update();
}
// Rekursive Methode zum Zeichnen des Baumes
void RenderArea::drawRect(double leftPointX, double leftPointY, double topPointX, double topPointY, int depth)
{
double leftWinkel = this->leftWinkel;
if(depth%2 && mutation == true)
{
leftWinkel = 90.0-leftWinkel;
}
QPainter painter(this);
painter.setPen(pen);
painter.setBrush(brush);
if(antialiased == true)
{
painter.setRenderHint(QPainter::Antialiasing, true);
}
double dx = topPointX - leftPointX;
double dy = leftPointY - topPointY;
double x3 = leftPointX - dy;
double y3 = leftPointY - dx;
double x4 = topPointX - dy;
double y4 = topPointY - dx;
double abs_dx = abs(topPointX - leftPointX);
double abs_dy = abs(leftPointY - topPointY);
// Abbruchbedingung, wenn die einzelnen Quadrate zu klein werden
if(abs_dx <= 0 && abs_dy <= 0)
{
return;
}
QPoint polygon[4] = {
QPoint(x3, y3),
QPoint(x4, y4),
QPoint(topPointX, topPointY),
QPoint(leftPointX, leftPointY)
};
painter.drawPolygon(polygon, 4);
// Abbruchbedingung
depth--;
if(depth == 0)
{
return;
}
// Berechnen des neuen Punktes
double beta = (atan(abs_dy/abs_dx))*180.0/M_PI;
double ht = sqrt(abs_dx*abs_dx+abs_dy*abs_dy);
double newHt = cos(leftWinkel*M_PI/180.0) * ht;
double gama;
if((x4>=x3 && y4<=y3) || (x4<x3 && y4>y3))
{
gama = leftWinkel + beta;
}
else
{
gama = leftWinkel - beta;
}
double dzx = cos(gama*M_PI/180.0) * newHt;
double dzy = sin(gama*M_PI/180.0) * newHt;
double zx = 0, zy = 0;
if(x4>=x3)
{
zx = x3 + dzx;
zy = y3 - dzy;
}
else
{
zx = x3 - dzx;
zy = y3 + dzy;
}
// Debug
//std::cout << "dzx / dzy: \t" << dzx << " / " << dzy;
//std::cout << "\t\tzx / zy: \t\t" << zx << " / " << zy << std::endl;
// Funktion erneut aufrufen
drawRect(x3, y3, zx, zy, depth);
drawRect(zx, zy, x4, y4, depth);
}
// Standard Zeichenevent
void RenderArea::paintEvent(QPaintEvent *)
{
// Kantenlänge des Basisquadrats in abhängigkeit des Zooms
double rectWidth;
if(width() <= height())
{
rectWidth = width()/zoom;
}
else
{
rectWidth = height()/zoom;
}
// Punkte für Polygon
double x1 = width()/2-rectWidth/2, y1 = height()-5; // bottom left
double x3 = x1+rectWidth; // top right
double x4 = x3, y4 = y1; // bottom right
// Debug
std::cout << "*------------- ---------------*" << std::endl;
std::cout << "Zoom: " << zoom << std::endl;
// erster aufruf der Funktion zum zeichnen
drawRect(x1, y1, x4, y4, maxDepth);
}