├── .gitignore ├── spirograph ├── 1 ├── 2 ├── README.md ├── Makefile └── main.cpp ├── sortingAlgorithms ├── test ├── Makefile └── main.cpp ├── .template ├── Makefile └── main.cpp ├── gravity ├── Makefile └── main.cpp ├── mandelbrot ├── Makefile └── main.cpp ├── triangles ├── Makefile ├── backup.cpp └── main.cpp └── magnetCursor ├── Makefile └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | *.o 3 | -------------------------------------------------------------------------------- /spirograph/README.md: -------------------------------------------------------------------------------- 1 | # SFML-Spirograph -------------------------------------------------------------------------------- /spirograph/2: -------------------------------------------------------------------------------- 1 | + 255 0 0 20 2 | + 0 0 255 20 3 | = 96 80 1 270 1 4 | 5 | -------------------------------------------------------------------------------- /sortingAlgorithms/test: -------------------------------------------------------------------------------- 1 | 0 1 0 2 | 1 2 0 3 | 2 3 0 4 | 3 4 0 5 | 4 5 0 6 | 5 1 1 7 | 6 2 1 8 | 7 3 1 9 | 8 4 1 10 | 9 1 2 11 | 10 2 2 12 | 11 3 2 13 | 12 1 2 14 | 13 2 2 15 | 14 1 3 16 | -------------------------------------------------------------------------------- /spirograph/1: -------------------------------------------------------------------------------- 1 | + 33 60 132 20 2 | + 48 88 188 20 3 | + 195 175 227 20 4 | + 108 86 169 20 5 | + 230 102 175 20 6 | + 200 85 96 20 7 | + 236 102 93 20 8 | + 235 73 63 20 9 | + 208 128 91 20 10 | + 234 180 159 20 11 | + 237 211 83 20 12 | # rot rin wh ho st# 13 | = -90 105 63 21 -2 14 | -------------------------------------------------------------------------------- /.template/Makefile: -------------------------------------------------------------------------------- 1 | CC = clang++ 2 | CFLAGS = -Wall -std=c++11 3 | LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system 4 | 5 | all: main clean 6 | 7 | main: main.o 8 | $(CC) -o $@.out $^ $(LDFLAGS) 9 | 10 | main.o: main.cpp 11 | $(CC) -c $(CFLAGS) $< 12 | 13 | clean: 14 | rm *.o 15 | 16 | -------------------------------------------------------------------------------- /gravity/Makefile: -------------------------------------------------------------------------------- 1 | CC = clang++ 2 | CFLAGS = -Wall -std=c++11 3 | LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system 4 | 5 | all: main clean 6 | 7 | main: main.o 8 | $(CC) -o $@.out $^ $(LDFLAGS) 9 | 10 | main.o: main.cpp 11 | $(CC) -c $(CFLAGS) $< 12 | 13 | clean: 14 | rm *.o 15 | 16 | -------------------------------------------------------------------------------- /mandelbrot/Makefile: -------------------------------------------------------------------------------- 1 | CC = clang++ 2 | CFLAGS = -Wall -std=c++11 3 | LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system 4 | 5 | all: main clean 6 | 7 | main: main.o 8 | $(CC) -o $@.out $^ $(LDFLAGS) 9 | 10 | main.o: main.cpp 11 | $(CC) -c $(CFLAGS) $< 12 | 13 | clean: 14 | rm *.o 15 | 16 | -------------------------------------------------------------------------------- /spirograph/Makefile: -------------------------------------------------------------------------------- 1 | CC = clang++ 2 | CFLAGS = -Wall -std=c++11 3 | LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system 4 | 5 | all: main clean 6 | 7 | main: main.o 8 | $(CC) -o $@.out $^ $(LDFLAGS) 9 | 10 | main.o: main.cpp 11 | $(CC) -c $(CFLAGS) $< 12 | 13 | clean: 14 | rm *.o 15 | 16 | -------------------------------------------------------------------------------- /triangles/Makefile: -------------------------------------------------------------------------------- 1 | CC = clang++ 2 | CFLAGS = -Wall -std=c++11 3 | LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system 4 | 5 | all: main clean 6 | 7 | main: main.o 8 | $(CC) -o $@.out $^ $(LDFLAGS) 9 | 10 | main.o: main.cpp 11 | $(CC) -c $(CFLAGS) $< 12 | 13 | clean: 14 | rm *.o 15 | 16 | -------------------------------------------------------------------------------- /magnetCursor/Makefile: -------------------------------------------------------------------------------- 1 | CC = clang++ 2 | CFLAGS = -Wall -std=c++11 3 | LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system 4 | 5 | all: main clean 6 | 7 | main: main.o 8 | $(CC) -o $@.out $^ $(LDFLAGS) 9 | 10 | main.o: main.cpp 11 | $(CC) -c $(CFLAGS) $< 12 | 13 | clean: 14 | rm *.o 15 | 16 | -------------------------------------------------------------------------------- /sortingAlgorithms/Makefile: -------------------------------------------------------------------------------- 1 | CC = clang++ 2 | CFLAGS = -Wall -std=c++11 3 | LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system 4 | 5 | all: main clean 6 | 7 | main: main.o 8 | $(CC) -o $@.out $^ $(LDFLAGS) 9 | 10 | main.o: main.cpp 11 | $(CC) -c $(CFLAGS) $< 12 | 13 | clean: 14 | rm *.o 15 | 16 | -------------------------------------------------------------------------------- /.template/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | sf::Vector2i dimensions(800, 600); 5 | 6 | int main() { 7 | sf::RenderWindow window(sf::VideoMode(dimensions.x, dimensions.y), ""); 8 | window.setFramerateLimit(30); 9 | 10 | while(window.isOpen()) { 11 | sf::Event event; 12 | while(window.pollEvent(event)) { 13 | switch(event.type) { 14 | case sf::Event::Closed: 15 | window.close(); 16 | break; 17 | default: break; 18 | } 19 | } 20 | 21 | window.clear(sf::Color::Black); 22 | 23 | window.display(); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /triangles/backup.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void draw(sf::VertexArray& points, int x, int y, int r, float m) { 5 | //std::cout << "draw" << std::endl; 6 | if(m >= 1 || m <= 0) return; 7 | 8 | if(r >= 10) { 9 | draw(points, x-r, y, r*m, m); 10 | draw(points, x+r, y, r*m, m); 11 | draw(points, x, y-r, r*m, m); 12 | } else { 13 | sf::Vector2f point(x, y); 14 | points.append(point); 15 | } 16 | 17 | } 18 | 19 | int main() { 20 | int width = 800; 21 | int height = 600; 22 | sf::RenderWindow window(sf::VideoMode(width, height), "Triangles"); 23 | window.setFramerateLimit(10); 24 | 25 | while(window.isOpen()) { 26 | sf::Event event; 27 | while(window.pollEvent(event)) { 28 | switch(event.type) { 29 | case sf::Event::Closed: 30 | window.close(); 31 | break; 32 | default: break; 33 | } 34 | } 35 | 36 | window.clear(sf::Color::Black); 37 | sf::VertexArray points(sf::Points); 38 | draw(points, width/2, height-100*2, 100, float(sf::Mouse::getPosition(window).x)/width); 39 | window.draw(points); 40 | 41 | window.display(); 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /triangles/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void draw(sf::RenderWindow& window, int x, int y, int r, float m) { 5 | //std::cout << "draw" << std::endl; 6 | if(m >= 1 || m <= 0) return; 7 | 8 | if(r >= 3) { 9 | draw(window, x-r, y, r*m, m); 10 | draw(window, x+r, y, r*m, m); 11 | draw(window, x, y-r, r*m, m); 12 | } 13 | sf::CircleShape c(r); 14 | c.setOrigin(r,r); 15 | c.setPosition(x, y); 16 | c.setFillColor(sf::Color::Transparent); 17 | c.setOutlineColor(sf::Color::Red); 18 | c.setOutlineThickness(2); 19 | window.draw(c); 20 | } 21 | 22 | int main() { 23 | int width = 800; 24 | int height = 600; 25 | sf::RenderWindow window(sf::VideoMode(width, height), "Triangles"); 26 | window.setFramerateLimit(10); 27 | 28 | while(window.isOpen()) { 29 | sf::Event event; 30 | while(window.pollEvent(event)) { 31 | switch(event.type) { 32 | case sf::Event::Closed: 33 | window.close(); 34 | break; 35 | default: break; 36 | } 37 | } 38 | 39 | window.clear(sf::Color::Black); 40 | draw(window, width/2, height-100*2, 100, float(sf::Mouse::getPosition(window).x)/width/2); 41 | 42 | window.display(); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /magnetCursor/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int height = 720; 6 | int width = height*16/9; 7 | 8 | float pixelPerM = 720; 9 | float gravityX = 0, gravityY = 0; 10 | 11 | float mouseForce = 0.01; 12 | 13 | sf::Vector2i mouse; 14 | 15 | sf::Clock clock; 16 | float elapsedTime; 17 | 18 | sf::RenderWindow window(sf::VideoMode(width, height), "SFML works!"); 19 | 20 | sf::CircleShape ball(100.f); 21 | ball.setFillColor(sf::Color::Green); 22 | ball.setOrigin(ball.getRadius(), ball.getRadius()); 23 | 24 | float ballX = width/2 - ball.getRadius()/2; 25 | float ballY = height/2 - ball.getRadius()/2; 26 | float ballDx = 0; 27 | float ballDy = 9.81; 28 | 29 | clock.restart(); 30 | while (window.isOpen()) { 31 | elapsedTime = clock.restart().asSeconds(); 32 | 33 | // INPUT HANDLING 34 | 35 | mouse = sf::Mouse::getPosition(window); 36 | 37 | sf::Event event; 38 | while (window.pollEvent(event)) { 39 | switch (event.type) { 40 | case sf::Event::Closed: 41 | window.close(); break; 42 | case sf::Event::MouseButtonPressed: 43 | ballX = mouse.x; 44 | ballY = mouse.y; 45 | ballDx = 0; 46 | ballDy = 0; 47 | break; 48 | case sf::Event::MouseWheelScrolled: 49 | mouseForce += 0.01 * event.mouseWheelScroll.delta; 50 | std::cout << mouseForce << " " << event.mouseWheelScroll.delta << std::endl; 51 | break; 52 | default: break; 53 | } 54 | } 55 | 56 | // LOGIC 57 | 58 | // mouse 59 | ballDx += (mouse.x - ball.getPosition().x) * mouseForce * elapsedTime; 60 | ballDy += (mouse.y - ball.getPosition().y) * mouseForce * elapsedTime; 61 | 62 | // gravity 63 | ballDy += gravityY * elapsedTime; 64 | ballDx += gravityX * elapsedTime; 65 | 66 | // move position based on speed 67 | ballX += ballDx * pixelPerM * elapsedTime; 68 | ballY += ballDy * pixelPerM * elapsedTime; 69 | 70 | // set sprite position to variable position 71 | ball.setPosition(ballX, ballY); 72 | 73 | // RENDERING 74 | window.clear(); 75 | window.draw(ball); 76 | window.display(); 77 | } 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /gravity/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | sf::Vector2i dimensions(800, 600); 6 | 7 | std::random_device rd; 8 | std::mt19937 rng(rd()); 9 | std::uniform_real_distribution randomFloat(0, 1); 10 | 11 | 12 | // PARTICLE 13 | 14 | class Particle { 15 | sf::Vector2f position; 16 | sf::Vector2f velocity; 17 | sf::Vector2f acceleration; 18 | 19 | public: 20 | Particle(const sf::Vector2f &p); 21 | Particle(const sf::Vector2f &p, sf::Vector2f &v); 22 | void update(const float deltaTime); 23 | void applyForce(const sf::Vector2f &force); 24 | void draw(sf::RenderWindow &window) const; 25 | }; 26 | 27 | Particle::Particle(const sf::Vector2f &p) : position(p), velocity(0,0) {} 28 | Particle::Particle(const sf::Vector2f &p, sf::Vector2f &v) : position(p), velocity(v) {} 29 | 30 | void Particle::update(const float deltaTime) { 31 | velocity += acceleration * deltaTime; 32 | position += velocity * deltaTime; 33 | acceleration *= 0.f; 34 | 35 | if(position.x < 0) { 36 | velocity.x *= -1.f; 37 | position.x = 0; 38 | } 39 | if(position.x > dimensions.x) { 40 | velocity.x *= -1.f; 41 | position.x = dimensions.x; 42 | } 43 | if(position.y < 0) { 44 | velocity.y *= -1.f; 45 | position.y = 0; 46 | } 47 | if(position.y > dimensions.y) { 48 | velocity.y *= -1.f; 49 | position.y = dimensions.y; 50 | } 51 | } 52 | 53 | void Particle::applyForce(const sf::Vector2f &force) { 54 | acceleration += force; 55 | } 56 | 57 | void Particle::draw(sf::RenderWindow &window) const { 58 | sf::CircleShape c(5); 59 | c.setOrigin(5, 5); 60 | c.setPosition(position); 61 | c.setFillColor(sf::Color::White); 62 | window.draw(c); 63 | } 64 | 65 | // MAIN 66 | 67 | int main() { 68 | std::vector particles; 69 | for(int i = 0; i < 10; i++) { 70 | particles.push_back(Particle(sf::Vector2f(randomFloat(rng)*dimensions.x, randomFloat(rng)*dimensions.y))); 71 | } 72 | 73 | sf::RenderWindow window(sf::VideoMode(dimensions.x, dimensions.y), "Physics"); 74 | window.setFramerateLimit(30); 75 | 76 | sf::Clock clock; 77 | float deltaTime(clock.getElapsedTime().asSeconds()); 78 | 79 | while(window.isOpen()) { 80 | deltaTime = clock.restart().asSeconds(); 81 | 82 | sf::Event event; 83 | while(window.pollEvent(event)) { 84 | switch(event.type) { 85 | case sf::Event::Closed: 86 | window.close(); 87 | break; 88 | default: break; 89 | } 90 | } 91 | 92 | for(Particle &p : particles) { 93 | p.applyForce(sf::Vector2f(100, 900)); 94 | p.update(deltaTime); 95 | } 96 | 97 | window.clear(sf::Color::Black); 98 | 99 | for(Particle &p : particles) { 100 | p.draw(window); 101 | } 102 | 103 | window.display(); 104 | } 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /sortingAlgorithms/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | sf::Vector2i dimensions(800, 600); 7 | 8 | int fps = 5; 9 | int stepsPerFrame = 1; 10 | int maximumValue = dimensions.y; 11 | int values = 15; 12 | int gapSize = 1; 13 | 14 | // Setup random numbers 15 | std::random_device rd; 16 | std::mt19937_64 generator(rd()); 17 | std::uniform_int_distribution valueRange(0, maximumValue); 18 | 19 | 20 | 21 | // SORTER 22 | 23 | class Sorter { 24 | protected: 25 | int barWidth; 26 | int step = 0; 27 | bool finished = false; 28 | std::vector items; 29 | std::vector colors; 30 | 31 | void swap(const int i1, const int i2); 32 | void calculateBarWidth(); 33 | void clearColors(); 34 | int getHeigthFromValue(const int value) const; 35 | 36 | Sorter(const std::vector &i); 37 | Sorter(); 38 | 39 | public: 40 | void generate(std::random_device &rd, std::uniform_int_distribution &valueRange); 41 | void draw(sf::RenderWindow &window); 42 | bool isFinished() const; 43 | 44 | virtual void next() = 0; 45 | }; 46 | 47 | Sorter::Sorter(const std::vector &i) : items(i), colors(i.size()) { 48 | calculateBarWidth(); 49 | clearColors(); 50 | } 51 | 52 | Sorter::Sorter() : items() { } 53 | 54 | void Sorter::generate(std::random_device &rd, std::uniform_int_distribution &valueRange) { 55 | items.clear(); 56 | for(int i = 0; i < values; i++) { 57 | items.push_back(valueRange(rd)); 58 | } 59 | calculateBarWidth(); 60 | clearColors(); 61 | } 62 | 63 | void Sorter::swap(const int i1, const int i2) { 64 | int temp = items[i1]; 65 | items[i1] = items[i2]; 66 | items[i2] = temp; 67 | 68 | colors[i1] = sf::Color::Yellow; 69 | colors[i2] = sf::Color::Yellow; 70 | } 71 | 72 | void Sorter::draw(sf::RenderWindow &window) { 73 | sf::RectangleShape rect; 74 | for(unsigned long i = 0; i < values; i++) { 75 | int barHeight = getHeigthFromValue(items[i]); 76 | rect.setPosition(i * barWidth + i * gapSize, dimensions.y - barHeight); 77 | rect.setSize(sf::Vector2f(barWidth, barHeight)); 78 | rect.setFillColor(colors[i]); 79 | window.draw(rect); 80 | } 81 | } 82 | 83 | 84 | void Sorter::calculateBarWidth() { 85 | barWidth = dimensions.x / values - gapSize; 86 | } 87 | 88 | void Sorter::clearColors() { 89 | colors.clear(); 90 | for(int i = 0; i < values; i++) colors.push_back(sf::Color::White); 91 | } 92 | 93 | int Sorter::getHeigthFromValue(const int value) const { 94 | return value * dimensions.y / maximumValue; 95 | } 96 | 97 | bool Sorter::isFinished() const { 98 | return finished; 99 | } 100 | 101 | 102 | 103 | // BUBBLESORTER 104 | 105 | class BubbleSorter : public Sorter { 106 | int index = 1; 107 | int passes = 0; 108 | public: 109 | BubbleSorter(const std::vector &i); 110 | BubbleSorter(); 111 | void next(); 112 | }; 113 | 114 | BubbleSorter::BubbleSorter(const std::vector &i) : Sorter(i) {} 115 | BubbleSorter::BubbleSorter() : Sorter() {} 116 | 117 | void BubbleSorter::next() { 118 | if(finished) return; 119 | clearColors(); 120 | 121 | index += 1; 122 | if(index >= values - passes) { 123 | index = 1; 124 | passes++; 125 | if(passes >= values) finished = true; 126 | } 127 | if(index >= values - passes) passes++; 128 | 129 | if(items[index-1] > items[index]) { 130 | swap(index-1, index); 131 | } else colors[index] = sf::Color::Red; 132 | step++; 133 | 134 | for(int i = values - passes; i < values; i++) { 135 | colors[i] = sf::Color::Green; 136 | } 137 | } 138 | 139 | 140 | 141 | 142 | // MAIN 143 | 144 | int main() { 145 | 146 | BubbleSorter sorter; 147 | sorter.generate(rd, valueRange); 148 | 149 | sf::RenderWindow window(sf::VideoMode(dimensions.x, dimensions.y), "SortingAlgorithms"); 150 | window.setFramerateLimit(fps); 151 | 152 | while(window.isOpen()) { 153 | sf::Event event; 154 | while(window.pollEvent(event)) { 155 | switch(event.type) { 156 | case sf::Event::Closed: 157 | window.close(); 158 | break; 159 | case sf::Event::KeyPressed: 160 | switch(event.key.code) { 161 | case sf::Keyboard::Space: 162 | sorter.next(); 163 | break; 164 | default: break; 165 | } 166 | default: break; 167 | } 168 | } 169 | 170 | if(!sorter.isFinished()) { 171 | window.clear(sf::Color::Black); 172 | for(int i = 0; i < stepsPerFrame; i++) 173 | sorter.next(); 174 | sorter.draw(window); 175 | window.display(); 176 | } 177 | } 178 | return 0; 179 | } 180 | -------------------------------------------------------------------------------- /mandelbrot/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | sf::Vector2i dimensions(500, 500); 7 | float pixelPerUnit = 200; 8 | mpf_t centerComplexX; 9 | mpf_t centerComplexY; 10 | 11 | sf::VertexArray pixels; 12 | 13 | int maxIterations = 30; 14 | bool valid = false; 15 | 16 | void add(mpf_t& op1, double op2) { 17 | mpf_t op2Converted; 18 | mpf_init_set_d(op2Converted, op2); 19 | mpf_add(op1, op1, op2Converted); 20 | } 21 | void sub(mpf_t& op1, double op2) { 22 | mpf_t op2Converted; 23 | mpf_init_set_d(op2Converted, op2); 24 | mpf_sub(op1, op1, op2Converted); 25 | } 26 | 27 | void getComplexFromPixelX(int pixelX, mpf_t& complexX) { 28 | mpf_t complexX_temp; 29 | mpf_init_set_d(complexX_temp, double(pixelX) / pixelPerUnit); 30 | mpf_add(complexX, complexX_temp, centerComplexX); 31 | sub(complexX, dimensions.x/2.0/pixelPerUnit); 32 | } 33 | 34 | void getComplexFromPixelY(int pixelY, mpf_t& complexY) { 35 | mpf_t complexY_temp; 36 | mpf_init_set_d(complexY_temp, double(pixelY) / pixelPerUnit); 37 | mpf_add(complexY, complexY_temp, centerComplexY); 38 | sub(complexY, dimensions.y/2.0/pixelPerUnit); 39 | } 40 | 41 | int getIterations(mpf_t cX, mpf_t cY) { 42 | mpf_t zX; 43 | mpf_t zY; 44 | mpf_t zX_temp; 45 | mpf_t zY_temp; 46 | mpf_init(zX); 47 | mpf_init(zY); 48 | mpf_init(zX_temp); 49 | mpf_init(zY_temp); 50 | for(int iteration = 1; iteration <= maxIterations; iteration++) { 51 | mpf_mul(zX_temp, zX, zX); 52 | mpf_mul(zY_temp, zY, zY); 53 | mpf_sub(zX_temp, zX_temp, zY_temp); 54 | 55 | mpf_mul(zY_temp, zX, zY); 56 | mpf_add(zY_temp, zY_temp, zY_temp); 57 | 58 | mpf_add(zX, zX_temp, cX); 59 | mpf_add(zY, zY_temp, cY); 60 | 61 | mpf_mul(zX_temp, zX, zX); 62 | mpf_mul(zY_temp, zY, zY); 63 | mpf_add(zX_temp, zX_temp, zY_temp); 64 | if(mpf_cmp_d(zX_temp, 4) > 0) 65 | return iteration; 66 | } 67 | return maxIterations; 68 | } 69 | 70 | int getIterations(sf::Vector2i pixel) { 71 | mpf_t x; 72 | mpf_t y; 73 | mpf_init(x); 74 | mpf_init(y); 75 | getComplexFromPixelX(pixel.x, x); 76 | getComplexFromPixelY(pixel.y, y); 77 | return getIterations(x, y); 78 | } 79 | 80 | sf::Color getPixelColor(sf::Vector2i pixel) { 81 | int iterations = getIterations(pixel); 82 | if(iterations == maxIterations) return sf::Color::Black; 83 | int color = (float(iterations) / maxIterations) * 255; 84 | return sf::Color(color, color, color); 85 | } 86 | 87 | void getPixels() { 88 | if(valid) return; 89 | pixels = sf::VertexArray(); 90 | for(int x = 0; x < dimensions.x; x++) { 91 | for(int y = 0; y < dimensions.y; y++) { 92 | sf::Color color = getPixelColor(sf::Vector2i(x, y)); 93 | sf::Vertex pixel(sf::Vector2f(x, y), color); 94 | pixels.append(pixel); 95 | } 96 | } 97 | valid = true; 98 | } 99 | 100 | int main() { 101 | mpf_init_set_d(centerComplexX, 0.001643721971153); 102 | mpf_init_set_d(centerComplexY, 0.822467633298876); 103 | 104 | 105 | sf::RenderWindow window(sf::VideoMode(dimensions.x, dimensions.y), "Mandelbrot"); 106 | window.setFramerateLimit(0); 107 | 108 | while(window.isOpen()) { 109 | sf::Event event; 110 | while(window.pollEvent(event)) { 111 | switch(event.type) { 112 | case sf::Event::Closed: 113 | window.close(); 114 | break; 115 | case sf::Event::Resized: 116 | dimensions.x = event.size.width; 117 | dimensions.y = event.size.height; 118 | window.setView(sf::View(sf::FloatRect(0, 0, dimensions.x, dimensions.y))); 119 | valid = false; 120 | break; 121 | case sf::Event::MouseButtonPressed: 122 | switch(event.mouseButton.button) { 123 | case sf::Mouse::Button::Left: 124 | getComplexFromPixelX(event.mouseButton.x, centerComplexX); 125 | getComplexFromPixelY(event.mouseButton.y, centerComplexY); 126 | valid = false; 127 | break; 128 | case sf::Mouse::Button::Right: 129 | maxIterations += 5; 130 | valid = false; 131 | break; 132 | default: break; 133 | } 134 | break; 135 | case sf::Event::KeyPressed: 136 | switch(event.key.code){ 137 | case sf::Keyboard::Space: 138 | { 139 | std::string input = ""; 140 | std::cout << "Move to:\na: "; 141 | std::cin >> input; 142 | mpf_set_str(centerComplexX, input.c_str(), 10); 143 | std::cout << "b: "; 144 | std::cin >> input; 145 | mpf_set_str(centerComplexY, input.c_str(), 10); 146 | valid = false; 147 | break; 148 | } 149 | case sf::Keyboard::Return: 150 | std::cout << "dimensions.x " << dimensions.x << std::endl 151 | << "dimensions.y " << dimensions.y << std::endl 152 | << "pixelPerUnit " << pixelPerUnit << std::endl 153 | << "maxIterations " << maxIterations << std::endl; 154 | default: break; 155 | } 156 | case sf::Event::MouseWheelScrolled: 157 | { 158 | int delta = event.mouseWheelScroll.delta; 159 | if(delta == 0) break; 160 | else if(delta < 0) { 161 | pixelPerUnit /= -delta * 1.1; 162 | } else { 163 | pixelPerUnit *= delta * 1.1; 164 | } 165 | 166 | valid = false; 167 | break; 168 | } 169 | default: break; 170 | } 171 | } 172 | 173 | getPixels(); 174 | window.clear(sf::Color::Black); 175 | window.draw(pixels); 176 | window.display(); 177 | } 178 | 179 | return 0; 180 | } 181 | -------------------------------------------------------------------------------- /spirograph/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // Window dimensions 7 | sf::Vector2i dimensions(800, 600); 8 | 9 | // Define colors others apart from palette 10 | sf::Color fgColor; 11 | sf::Color bgColor = sf::Color::White; 12 | sf::Color fxColor = sf::Color(200, 200, 200); 13 | 14 | // Width of the lines 15 | float lineWidth = 2; 16 | float paintWidth = 2; 17 | 18 | // This file contains ring, wheel, hole and other config data 19 | std::ifstream configFile; 20 | 21 | // Change speed with mouse wheel 22 | float degreesPerFrame = 1; 23 | float degreesPerFrameStep = 0.1; 24 | 25 | float lengthMultiplier; 26 | std::vector lines(3); 27 | 28 | // Values for spirograph 29 | float ring; 30 | float wheel; 31 | float hole; 32 | float step; 33 | 34 | // Keep ring's and wheel's last rotation to calculate full rotations 35 | float lastRingRotation; 36 | float lastWheelRotation; 37 | 38 | // Angle at which the spirograph starts 39 | float rotation; 40 | 41 | // Palette to store all colors in 42 | std::vector palette; 43 | int paletteIndex; 44 | 45 | // Function declarations 46 | sf::Vector2f getRotatedVector(sf::RectangleShape shape, int point); 47 | int slowestLineIndex(std::vector degreesPerSecond); 48 | void updateLines(std::vector& lines); 49 | void drawCircleOutline(sf::RenderWindow& window, int x, int y, float r, sf::Color color); 50 | void nextConfig(); 51 | void nextHole(); 52 | 53 | int main() { 54 | // Take config path from std::cin 55 | std::string configFilePath; 56 | std::cin >> configFilePath; 57 | configFile.open(configFilePath); 58 | nextConfig(); 59 | 60 | lengthMultiplier = (dimensions.y/2 - 1)/ring; 61 | // Pixelarray for drawn pixels 62 | sf::VertexArray paint(sf::TrianglesStrip); 63 | 64 | // Create window 65 | sf::RenderWindow window(sf::VideoMode(dimensions.x, dimensions.y), "Spirograph"); 66 | window.setFramerateLimit(30); 67 | 68 | // Set rotation and color of all spirograph lines 69 | for(uint i = 0; i < lines.size(); i++) { 70 | lines[i] = sf::RectangleShape(); 71 | lines[i].rotate(rotation); 72 | lines[i].setFillColor(fxColor); 73 | } 74 | // Update line lengths from global variables ring, wheel and hole 75 | updateLines(lines); 76 | 77 | 78 | // Move first line to screen center 79 | lines[0].setPosition(sf::Vector2f(dimensions.x/2, dimensions.y/2)); 80 | 81 | bool linesVisible = true; 82 | 83 | // MAINLOOP 84 | while(window.isOpen()) { 85 | // Check for events 86 | sf::Event event; 87 | while(window.pollEvent(event)) { 88 | // Check if event is one of the following types 89 | switch(event.type) { 90 | // Close window 91 | case sf::Event::Closed: 92 | window.close(); 93 | break; 94 | // Scrollwheel to change degreesPerFrame 95 | case sf::Event::MouseWheelScrolled: 96 | { 97 | // Change degreesPerFrame by amount scrolled 98 | degreesPerFrame += event.mouseWheelScroll.delta * degreesPerFrameStep; 99 | // Check minimum and maximum 100 | if(degreesPerFrame < 0) degreesPerFrame = 0; 101 | 102 | // Hide lines if degreesPerFrame is 0 103 | if(degreesPerFrame == 0) linesVisible = false; 104 | else linesVisible = true; 105 | break; 106 | } 107 | case sf::Event::KeyPressed: 108 | switch(event.key.code) { 109 | case sf::Keyboard::Space: 110 | linesVisible = !linesVisible; 111 | break; 112 | case sf::Keyboard::N: 113 | nextHole(); 114 | break; 115 | case sf::Keyboard::Return: 116 | nextConfig(); 117 | break; 118 | default: break; 119 | } 120 | default: 121 | break; 122 | } 123 | } 124 | 125 | // LOGIC 126 | 127 | if(linesVisible != 0) { 128 | // Rotate each line 129 | lines[0].rotate(degreesPerFrame); 130 | lines[1].rotate(degreesPerFrame * -(ring - wheel) / wheel); 131 | lines[2].setRotation(lines[1].getRotation()); 132 | // Move each line 133 | for(uint i = 1; i < lines.size(); i++) { 134 | // Move position to the end of the previous line 135 | lines[i].setPosition(getRotatedVector(lines[i-1], 1)); 136 | } 137 | sf::Vector2f pos = getRotatedVector(lines.back(), 1); 138 | paint.append(sf::Vertex(sf::Vector2f(pos.x-paintWidth, pos.y), fgColor)); 139 | paint.append(sf::Vertex(sf::Vector2f(pos.x+paintWidth, pos.y), fgColor)); 140 | paint.append(sf::Vertex(sf::Vector2f(pos.x, pos.y-paintWidth), fgColor)); 141 | paint.append(sf::Vertex(sf::Vector2f(pos.x, pos.y+paintWidth), fgColor)); 142 | } 143 | 144 | float ringRotation = lines[0].getRotation(); 145 | float wheelRotation = lines[1].getRotation(); 146 | 147 | if(lastRingRotation < rotation && lastWheelRotation < rotation) { 148 | if(ringRotation >= rotation && wheelRotation >= rotation) 149 | std::cout << "SUCCESS" << std::endl; 150 | } 151 | //std::cout << lastRingRotation << " " << lastWheelRotation << " " << rotation << std::endl; 152 | //std::cout << ringRotation << " " << wheelRotation << "\n" << std::endl; 153 | 154 | lastRingRotation = ringRotation; 155 | lastWheelRotation = wheelRotation; 156 | // RENDER 157 | 158 | // Fill the window with background color 159 | window.clear(bgColor); 160 | 161 | // Draw each line 162 | window.draw(paint); 163 | if(linesVisible) { 164 | window.draw(lines[1]); 165 | sf::Vector2f pos(getRotatedVector(lines[1], 0)); 166 | drawCircleOutline(window, pos.x, pos.y, lines[1].getSize().x, fxColor); 167 | } 168 | 169 | drawCircleOutline(window, dimensions.x/2, dimensions.y/2, ring * lengthMultiplier, fxColor); 170 | 171 | // Swap buffers to show the image to the screen 172 | window.display(); 173 | } 174 | 175 | // Program ended without errors 176 | return 0; 177 | } 178 | 179 | sf::Vector2f getRotatedVector(sf::RectangleShape shape, int point) { 180 | // You can't just use the shape.getPoint() method, because it doesn't 181 | // take the transformations (rotations) into consideration 182 | return shape.getTransform().transformPoint(shape.getPoint(point)); 183 | } 184 | 185 | void updateLines(std::vector& lines) { 186 | float hole_temp = hole * 2.3 + 10; 187 | lines[0].setSize(sf::Vector2f((ring-wheel) * lengthMultiplier, lineWidth)); 188 | lines[1].setSize(sf::Vector2f(wheel * lengthMultiplier, lineWidth)); 189 | lines[2].setSize(sf::Vector2f(-hole_temp * lengthMultiplier, lineWidth)); 190 | } 191 | 192 | void drawCircleOutline(sf::RenderWindow& window, int x, int y, float r, sf::Color color) { 193 | sf::CircleShape circle(r, 100); 194 | circle.setOrigin(r, r); 195 | circle.setPosition(x, y); 196 | circle.setFillColor(sf::Color::Transparent); 197 | circle.setOutlineThickness(1); 198 | circle.setOutlineColor(color); 199 | window.draw(circle); 200 | } 201 | 202 | void nextConfig() { 203 | palette.clear(); 204 | std::string word; 205 | while(configFile >> word && word[0] != '=') { 206 | if(word[0] == '+') { 207 | float r, g, b, a; 208 | configFile >> r >> g >> b >> a; 209 | std::cout << r << " " << g << " " << b << " " << a << std::endl; 210 | palette.push_back(sf::Color(r, g, b, a)); 211 | } 212 | } 213 | configFile >> ring >> wheel >> hole >> rotation >> step; 214 | std::cout << ring << " " << wheel << " " << " " << hole << " " << rotation << " " << step << std::endl; 215 | paletteIndex = 0; 216 | fgColor = palette[paletteIndex]; 217 | } 218 | 219 | void nextHole() { 220 | fgColor = palette[++paletteIndex % palette.size()]; 221 | hole += step; 222 | updateLines(lines); 223 | } 224 | --------------------------------------------------------------------------------