-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRender.cpp
162 lines (117 loc) · 3.51 KB
/
Render.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
#include "Render.h"
#include <iostream>
#define WIDTH 800
#define HEIGHT 800
Render::Render(Properties properties, string imageName) {
this->properties = properties;
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
window = new sf::RenderWindow(sf::VideoMode(WIDTH, HEIGHT), "String Art", sf::Style::Default, settings);
points = generatePoints();
sf::Image image;
if (!image.loadFromFile(imageName))
cerr << "Couldn't read image";
lines = generateLines(points, image);
}
vector<sf::Vector2f> Render::getPixelsInLine(sf::Vector2f& startCoordinates, sf::Vector2f& endCoordinates) {
bool steep = false;
float x0 = startCoordinates.x;
float y0 = startCoordinates.y;
float x1 = endCoordinates.x;
float y1 = endCoordinates.y;
vector<sf::Vector2f> pixels;
if (abs(x0 - x1) < abs(y0 - y1)) {
swap(x0, y0);
swap(x1, y1);
steep = true;
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
int dx = x1 - x0;
int dy = y1 - y0;
int derror2 = std::abs(dy) * 2;
int error2 = 0;
int y = y0;
for (int x = x0; x <= x1; x++) {
if (steep) {
pixels.push_back(sf::Vector2f(y, x));
}
else {
pixels.push_back(sf::Vector2f(x, y));
}
error2 += derror2;
if (error2 > dx) {
y += (y1 > y0 ? 1 : -1);
error2 -= dx * 2;
}
}
return pixels;
}
vector<sf::Vector2f> Render::generatePoints() {
vector<sf::Vector2f> points;
const double divisionAngle = 2 * PI / properties.numberPoints;
for (int i = 0; i < properties.numberPoints; i++) {
float currentAngle = divisionAngle * i;
points.push_back(sf::Vector2f(cos(currentAngle) * (properties.radius - 1) + 400, sin(currentAngle) * (properties.radius - 1) + 400));
}
return points;
}
vector<pair<int, int>> Render::generateLines(vector<sf::Vector2f>& points, sf::Image& image) {
vector<pair<int, int>> lines;
int choosenPointIndex = 0;
vector<sf::Vector2f> pixelsToErase;
// Number of strings
for (int i = 0; i < properties.numberLines; i++) {
int startPointIndex = choosenPointIndex;
float bestScore = 0;
// Iterate over every point to see where to connect the string to
for (int j = 0; j < points.size(); j++) {
vector<sf::Vector2f> pixels = getPixelsInLine(points[startPointIndex], points[j]);
float score = 0;
for (sf::Vector2f pixel : pixels) {
sf::Color color = image.getPixel(pixel.x, pixel.y);
if (0.3 * color.r + 0.59 * color.g + 0.11 * color.r < 10)
score += 1;
}
if (score > bestScore) {
choosenPointIndex = j;
bestScore = score;
pixelsToErase = pixels;
}
}
int endPointIndex = choosenPointIndex;
pair<int, int> line = { startPointIndex, endPointIndex };
lines.push_back(line);
for (sf::Vector2f pixel : pixelsToErase)
image.setPixel(pixel.x, pixel.y, sf::Color::White);
}
return lines;
}
void Render::drawLine(sf::Vector2f& startCoordinates, sf::Vector2f& endCoordinates) {
sf::Vertex line[] = {
sf::Vertex(startCoordinates, sf::Color(0, 0, 0, properties.opacity)),
sf::Vertex(endCoordinates, sf::Color(0, 0, 0, properties.opacity))
};
window->draw(line, 2, sf::Lines);
}
void Render::draw() {
int count = 0;
int index = 0;
sf::Clock clock;
while (window->isOpen()) {
sf::Event event;
window->clear(sf::Color::White);
while (window->pollEvent(event))
if (event.type == sf::Event::Closed)
window->close();
if (clock.getElapsedTime().asMilliseconds() > 5 && index < properties.numberLines) {
index++;
clock.restart();
}
for (int i = 0; i < index; i++)
drawLine(points[lines[i].first], points[lines[i].second]);
window->display();
}
}