aboutsummaryrefslogtreecommitdiffstats
path: root/TriangleFractalChaos.cpp
blob: bb25c102193db50b0ae2a9702c52ad983706c076 (plain) (blame)
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
#include "TriangleFractalChaos.h"
#include <QPushButton>
#include <QSpinBox>
#include <QGridLayout>
#include <QLabel>
#include <QImage>
#include <QTimer>

TriangleFractalChaos::TriangleFractalChaos(QWidget *parent)
    : QWidget(parent)
{
    dots = 0;
    drawTimer = new QTimer(this);
    connect(drawTimer, SIGNAL(timeout()), this, SLOT(drawDot()));
    delayLabel = new QLabel("Delay");
    magnificationLabel = new QLabel("Magnification");
    delay = new QSpinBox;
    delay->setMinimum(1);
    delay->setValue(10);
    delay->setSuffix(" ms");
    magnification = new QSpinBox;
    magnification->setMinimum(1);
    magnification->setMaximum(10);
    magnification->setValue(2);
    magnification->setSuffix("x");
    generate = new QPushButton("&Generate!");
    connect(generate, SIGNAL(clicked()), this, SLOT(toggleDraw()));
    QGridLayout *grid = new QGridLayout;
    grid->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    grid->addWidget(delayLabel, 0, 0, Qt::AlignRight);
    grid->addWidget(delay, 0, 1);
    grid->addWidget(magnificationLabel, 1, 0, Qt::AlignRight);
    grid->addWidget(magnification, 1, 1);
    grid->addWidget(generate, 2, 0, 1, 2);
    setLayout(grid);
    resize(500, 500);
    setWindowTitle("Triangle Fractal Chaos");
}
void TriangleFractalChaos::toggleDraw()
{
    static uint backgroundColor = palette().color(QPalette::Window).rgb();
    if(delayLabel->isEnabled()) {
	delayLabel->setEnabled(false);
	delay->setEnabled(false);
	magnificationLabel->setEnabled(false);
	magnification->setEnabled(false);
	generate->setText("&Stop");
	setFixedSize(size());
	int width = qMin(this->width(), this->height());
	int height = qRound((double)width * 1.73205081 / 2.0);
	int deltaWidthHalf = qRound(((double)(this->width() - width)) / 2.0) / magnification->value();
	int deltaHeightHalf = qRound(((double)(this->height() - height)) / 2.0) / magnification->value();
	width /= magnification->value();
	height /= magnification->value();
	if(dots) 
	    delete dots;
	dots = new QImage(size() / magnification->value(), QImage::Format_RGB32);
	dots->fill(backgroundColor);
	startingPoints[0] = QPoint(qRound((double)width / 2.0) - 1 + deltaWidthHalf, deltaHeightHalf);
	startingPoints[1] = QPoint(deltaWidthHalf, height - 1 + deltaHeightHalf);
	startingPoints[2] = QPoint(width - 1 + deltaWidthHalf, height - 1 + deltaHeightHalf);
	dots->setPixel(startingPoints[0], 0x00ff00);
	dots->setPixel(startingPoints[1], 0x00ff00);
	dots->setPixel(startingPoints[2], 0x00ff00);
	int randomX = qrand() % (width + 1);
	int yStart = qAbs(height - qRound((double)height / ((double)width / 2.0) * (double)randomX));
	int randomY = (qrand() % (height - yStart + 1)) + yStart;
	lastPoint = QPoint(randomX + deltaWidthHalf, randomY + deltaHeightHalf);
	dots->setPixel(lastPoint, 0xff0000);
	drawTimer->start(delay->value());
    } else {
	drawTimer->stop();
	delayLabel->setEnabled(true);
	delay->setEnabled(true);
	magnificationLabel->setEnabled(true);
	magnification->setEnabled(true);
	generate->setText("&Generate!");
	setMinimumSize(0, 0);
	setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
    }
}
void TriangleFractalChaos::drawDot()
{
    lastPoint = (lastPoint + startingPoints[qrand() % 3]) / 2;
    dots->setPixel(lastPoint, 0x000000);
    QPalette palette;
    palette.setBrush(backgroundRole(), dots->scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
    setPalette(palette);
}