-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
67 lines (56 loc) · 2.47 KB
/
main.cpp
File metadata and controls
67 lines (56 loc) · 2.47 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
#include <iostream>
#include <SFML/Graphics.hpp>
#include <vector>
#include <cmath>
#include <random>
#include "HilbertCurve.hpp"
// Function to generate a random color
sf::Color getRandomColor() {
static std::mt19937 rng(std::random_device{}());
static std::uniform_int_distribution<int> dist(0, 255);
return sf::Color(dist(rng), dist(rng), dist(rng));
}
// Function to create a line with specified thickness
sf::RectangleShape createLine(sf::Vector2f start, sf::Vector2f end, float thickness, sf::Color color) {
sf::Vector2f direction = end - start;
float length = std::sqrt(direction.x * direction.x + direction.y * direction.y);
sf::RectangleShape line(sf::Vector2f(length, thickness));
line.setFillColor(color);
line.setPosition(start);
line.setRotation(std::atan2(direction.y, direction.x) * 180 / 3.14159);
return line;
}
int main() {
int order = 4; // Order of the Hilbert curve
int width = 512; // Width of the window
int height = 512; // Height of the window
int padding = 20; // Padding around the Hilbert curve
float lineThickness = 3.0f; // Thickness of the lines
HilbertCurve hilbertCurve(order, width, height, padding); // Create a HilbertCurve object with padding
auto curve = hilbertCurve.getCurve(); // Get the generated Hilbert curve points
// Create an SFML window to display the Hilbert curve
sf::RenderWindow window(sf::VideoMode(width, height), "Hilbert Curve");
// Generate random colors for each segment
std::vector<sf::Color> colors;
for (size_t i = 0; i < curve.size(); ++i) {
colors.push_back(getRandomColor());
}
// Main loop to keep the window open and display the curve
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close(); // Close the window if the close event is triggered
}
window.clear(sf::Color::White); // Clear the window with a white background
// Draw each segment of the Hilbert curve with a specified thickness
for (size_t i = 1; i < curve.size(); ++i) {
sf::Vector2f start(curve[i - 1].first, curve[i - 1].second);
sf::Vector2f end(curve[i].first, curve[i].second);
sf::RectangleShape line = createLine(start, end, lineThickness, colors[i]);
window.draw(line); // Draw the line segment
}
window.display(); // Display the window
}
return 0;
}