Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <SFML/Window.hpp>
- #include <SFML/System.hpp>
- #include <list>
- #include <math.h>
- #include <iostream>
- #define PI 3.14159265
- using namespace std;
- struct Point
- {
- float x = 0;
- float y = 0;
- };
- void drawBezier(Point p1, Point p2, Point p3, sf::RenderWindow &window);
- int main()
- {
- // Fenster öffnen
- sf::RenderWindow window(sf::VideoMode(1920, 1080), "Bezier Curve", sf::Style::Fullscreen);
- // Hintergrundbild
- sf::Sprite s;
- sf::IntRect r;
- sf::Texture t;
- t.loadFromFile("Data/Graphics/Background3.png");
- r.top = 0.0f;
- r.left = 0.0f;
- r.width = 1920.0f;
- r.height = 1080.0f;
- s.setTexture(t);
- s.setTextureRect(r);
- window.draw(s);
- // Punkte
- Point p1;
- p1.x = 100.0f;
- p1.y = 1000.0f;
- Point p2;
- p2.x = 300.0f;
- p2.y = 100.0f;
- Point p3;
- p3.x = 500.0f;
- p3.y = 1000.0f;
- Point p4;
- p4.x = 700.0f;
- p4.y = 100.0f;
- Point p5;
- p5.x = 900.0f;
- p5.y = 1000.0f;
- drawBezier(p1, p2, p3, window);
- drawBezier(p3, p4, p5, window);
- window.display();
- bool beenden = false;
- while (!beenden)
- {
- // Events
- sf::Event e;
- if (window.pollEvent(e))
- {
- if (e.type = sf::Event::KeyPressed)
- if(e.key.code == sf::Keyboard::LAlt)
- beenden = true;
- }
- }
- return 0;
- }
- void drawBezier(Point p1, Point p2, Point p3, sf::RenderWindow &window)
- {
- std::list<Point> p;
- for (float t = 0.0f; t < 1; t += 0.01f)
- {
- Point P;
- P.x = std::pow(1 - t, 2) * p1.x + (1 - t) * 2 * t * p2.x + t * t * p3.x;
- P.y = std::pow(1 - t, 2) * p1.y + (1 - t) * 2 * t * p2.y + t * t * p3.y;
- p.push_back(P);
- }
- // Iterator
- std::list<Point>::iterator it = p.begin();
- Point lastPoint = *it;
- it++;
- while(it != p.end())
- {
- // SFML Graphic
- sf::Sprite *S = new sf::Sprite;
- sf::IntRect *R = new sf::IntRect;
- sf::Texture *T = new sf::Texture;
- // Winkel zwischen Punkten
- float gegenkathete = it->y - lastPoint.y;
- float ankathete = it->x - lastPoint.x;
- float hypothenuse = std::pow(gegenkathete, 2) + std::pow(ankathete, 2);
- float winkel = atan(gegenkathete / ankathete) * 180 / PI;
- T->loadFromFile("Data/Graphics/dot.png");
- R->top = 0.0f;
- R->left = 0.0f;
- R->width = 2.0f;
- R->height = 2.0f;
- S->setTexture(*T);
- S->setTextureRect(*R);
- S->setRotation(winkel);
- S->setScale(std::pow(hypothenuse, 0.5f) / 2.0f, 1);
- //S->setOrigin(R->width / 2, R->height / 2);
- S->setPosition(it->x, it->y);
- lastPoint = *it;
- it++;
- window.draw(*S);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement