#include "TriangleFractalChaos.h" #include #include #include #include #include #include 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); }