Snippets
Created by
Neural Outlet
last modified
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | #include "widget.h"
#include "ui_widget.h"
#include <QLabel>
#include <QGridLayout>
#include <QVector2D>
#include <QVector3D>
#include <QVector4D>
#include <QMatrix4x4>
#include <iostream>
#include <vector>
//Some global colours for ease.
QRgb g_Black = QColor::fromRgb(0,0,0).rgb();
QRgb g_Grey = QColor::fromRgb(127,127,127).rgb();
QRgb g_White = QColor::fromRgb(255,255,255).rgb();
//To keep colour with point
struct Point{
qreal _x,_y,_z;
QRgb _c;
Point(qreal x, qreal y, qreal z){
_x=x; _y=y; _z=z;
}
QVector3D ToQVec(){ return QVector3D(_x, _y, _z); }
};
//Tribonacci word generator
QString TribWord(int cycle)
{
QString word("1");
QString temp("");
for(int i = 0; i < cycle; ++i)
{
for(int j = 0; j < word.size(); ++j)
{
if(word[j] == '1')
temp += "12";
if(word[j] == '2')
temp += "13";
if(word[j] == '3')
temp += "1";
}
word = temp;
temp = "";
}
return word;
}
//The Orthographic Projection function:
QVector2D OrthoProj(QVector3D point)
{
//Unit cube
qreal left = -1, bottom = -1, nearPlane = 1;
qreal right = 1, top = 1, farPlane = -1;
// Construct the projection.
qreal width = right - left;
qreal invheight = top - bottom;
qreal clip = farPlane - nearPlane;
//Orthographic projection matrix
QGenericMatrix<4,4,qreal> m; m.fill(1);
m(0,0) = 2.0f / width;
m(1,0) = 0.0f;
m(2,0) = 0.0f;
m(3,0) = -(left + right) / width;
m(0,1) = 0.0f;
m(1,1) = 2.0f / invheight;
m(2,1) = 0.0f;
m(3,1) = -(top + bottom) / invheight;
m(0,2) = 0.0f;
m(1,2) = 0.0f;
m(2,2) = -2.0f / clip;
m(3,2) = -(nearPlane + farPlane) / clip;
m(0,3) = 0.0f;
m(1,3) = 0.0f;
m(2,3) = 0.0f;
m(3,3) = 1.0f;
QGenericMatrix<4,1,qreal> m2;
m2(0, 0) = point.x();
m2(0, 1) = point.y();
m2(0, 2) = point.z();
m2(0, 3) = 1.0;
// Apply the projection.
QGenericMatrix<4,1,qreal> m3 = m2 * m;
//std::cout << "(" << m2(0,0) << ", " << m2(0,1) << ", " << m2(0,2) << ", " << m2(0,3) << ")" << std::endl;
//std::cout << "times" << std::endl;
//std::cout << "|" << m(0,0) << ", " << m(0,1) << ", " << m(0,2) << ", " << m(0,3) << "|" << std::endl;
//std::cout << "|" << m(1,0) << ", " << m(1,1) << ", " << m(1,2) << ", " << m(1,3) << "|" << std::endl;
//std::cout << "|" << m(2,0) << ", " << m(2,1) << ", " << m(2,2) << ", " << m(2,3) << "|" << std::endl;
//std::cout << "|" << m(3,0) << ", " << m(3,1) << ", " << m(3,2) << ", " << m(3,3) << "|" << std::endl;
//std::cout << "equals" << std::endl;
//std::cout << "|" << m3(0,0) << ", " << m3(0,1) << ", " << m3(0,2) << ", " << m3(0,3) << "|" << std::endl;
return QVector2D(m3(0,0), m3(0,1));
}
//Takes the tribonacci word, iterates through it creating the
//staircase of 3D points, then uses the last one to scale the others
//and projects it onto a 2D plane.
QImage RauzyPlane(QString tribonacci)
{
Point end(0,0,0);
std::vector<Point> stairs;
std::vector<Point>::iterator iter = stairs.begin();
stairs.push_back(Point(0,0,0));
for(int i = 0; i < tribonacci.size(); ++i)
{
Point step = stairs[i];
if(tribonacci[i] == '1')
{
step._x += 1;
step._c = QColor::fromRgb(255,0,0).rgb();
}
if(tribonacci[i] == '2')
{
step._y += 1;
step._c = QColor::fromRgb(0,255,0).rgb();
}
if(tribonacci[i] == '3')
{
step._z += 1;
step._c = QColor::fromRgb(0,0,255).rgb();
}
stairs.push_back(step);
end = step;
}
std::cout << "# of stair points: " << stairs.size() << std::endl;
std::cout << "Stair end: " << end._x << ", " << end._y << ", " << end._z << std::endl;
//Create the image with a grey background
QImage img(100,100,QImage::Format_ARGB32);
for(int a = 0; a < 100; ++a)
{
for(int b = 0; b < 100; ++b)
{
img.setPixel(a,b,g_Grey);
}
}
//for each point n the staircase, project it to the 2D plane
for(iter = stairs.begin(); iter != stairs.end(); ++iter)
{
//Normalise the point by the end point
QVector3D proj = (*iter).ToQVec();
proj.setX(proj.x()/end._x);
proj.setY(proj.y()/end._y);
proj.setZ(proj.z()/end._z);
QVector2D pos = OrthoProj(proj);
//Scale for screen
qreal xOut = 50 + (pos.x() * 25);
qreal yOut = 50 + (pos.y() * 25);
if(xOut < 0 || xOut > 100)
std::cout << "X ERR: " << xOut << std::endl;
if(yOut < 0 || yOut > 100)
std::cout << "Y ERR: " << yOut << std::endl;
//Colout the new pixel with its corrosponding colour
img.setPixel(xOut, yOut, (*iter)._c);
}
return img;
}
// The actual widet
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowTitle("Not a Rauzy Fractal");
//Generate a tribonacci word of 15 cycles
QString trib = TribWord(15);
//Create fractal based on the word
QImage blah = RauzyPlane(trib);
//Set centre pixel as black
blah.setPixel(50,50,g_Black);
//Create label to show image
QLabel *label = new QLabel;
label->setPixmap(QPixmap::fromImage(blah.scaled(500,500)));
//Create layout and add the image
QGridLayout *lay = new QGridLayout;
lay->addWidget(label);
setLayout(lay);
show();
}
Widget::~Widget()
{
delete ui;
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.