-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
92 lines (82 loc) · 2.64 KB
/
main.cpp
File metadata and controls
92 lines (82 loc) · 2.64 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
#include "matrix.h"
#include "snowflake.h"
#include "draw.h"
#include "constants.h"
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstddef>
#include <SFML/Graphics.hpp>
using namespace lapiday;
using namespace std;
int main() {
srand(time(NULL));
vector<snowflake::line> lines;
vector<snowflake::line>::size_type linecount;
//Seed a single "spoke"
lines.push_back(snowflake::line(snowflake::scale(BASE_LENGTH)));
//Iterate
//vector<snowflake::line> newlines;
double distance; //Distance from parent line (0 to 1)
double scale; //Scale factor (0 to 1)
for(unsigned int i = 0; i < ITERATION_COUNT; i++) {
linecount = lines.size();
for(vector<snowflake::line>::size_type j = 0; j < linecount; j++) {
if(!lines[j].completed) {
for(unsigned int k = 0; k < PAIRS_PER_LINE; k++) {
//Distance must be in the k-th part
distance = ((static_cast<double>(rand()) / RAND_MAX) + k) / PAIRS_PER_LINE;
scale = MIN_SCALE + (static_cast<double>(rand()) / RAND_MAX) * (MAX_SCALE - MIN_SCALE);
lines.push_back(snowflake::line(lines[j].transformation * snowflake::translate(0, distance) * snowflake::rotate(PI / 3) * snowflake::scale(scale)));
lines.push_back(snowflake::line(lines[j].transformation * snowflake::translate(0, distance) * snowflake::rotate(-PI / 3) * snowflake::scale(scale)));
}
lines[j].completed = true;
}
}
}
//Create remaining "spokes"
linecount = lines.size();
for(vector<snowflake::line>::size_type i = 0; i < linecount; i++) {
for(int j = 1; j < 6; j++) {
lines.push_back(snowflake::line(snowflake::rotate(PI / 3 * j) * lines[i].transformation));
}
}
//Translate all points
linecount = lines.size();
for(vector<snowflake::line>::size_type i = 0; i < linecount; i++) {
lines[i].transformation = snowflake::translate(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2) * lines[i].transformation;
}
//Draw
#ifndef LAPIDAY_RENDER_TO_FILE
sf::RenderWindow target;
target.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Snowflake Test");
#else
sf::RenderTexture target;
if(!target.create(WINDOW_WIDTH, WINDOW_HEIGHT)) {
cout << "Could not create texture!" << endl;
return 1;
}
target.setSmooth(true);
#endif
target.clear(BACKGROUND_COLOR);
linecount = lines.size();
for(vector<snowflake::line>::size_type i = 0; i < linecount; i++) {
draw_line(target, lines[i], WINDOW_HEIGHT);
}
target.display();
#ifndef LAPIDAY_RENDER_TO_FILE
//Keep open
while(target.isOpen()) {
sf::Event e;
while(target.pollEvent(e)) {
if(e.type == sf::Event::Closed) {
target.close();
}
}
}
#else
target.getTexture().copyToImage().saveToFile("snowflake.png");
#endif
return 0;
}