Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Window.hpp>
  3. #include <SFML/System.hpp>
  4. #include <list>
  5. #include <math.h>
  6. #include <iostream>
  7.  
  8. #define PI 3.14159265
  9.  
  10.  
  11. using namespace std;
  12.  
  13. struct Point
  14. {
  15. float x = 0;
  16. float y = 0;
  17. };
  18.  
  19. void drawBezier(Point p1, Point p2, Point p3, sf::RenderWindow &window);
  20.  
  21. int main()
  22. {
  23.  
  24. // Fenster öffnen
  25. sf::RenderWindow window(sf::VideoMode(1920, 1080), "Bezier Curve", sf::Style::Fullscreen);
  26.  
  27. // Hintergrundbild
  28. sf::Sprite s;
  29. sf::IntRect r;
  30. sf::Texture t;
  31.  
  32. t.loadFromFile("Data/Graphics/Background3.png");
  33. r.top = 0.0f;
  34. r.left = 0.0f;
  35. r.width = 1920.0f;
  36. r.height = 1080.0f;
  37.  
  38. s.setTexture(t);
  39. s.setTextureRect(r);
  40. window.draw(s);
  41.  
  42. // Punkte
  43. Point p1;
  44. p1.x = 100.0f;
  45. p1.y = 1000.0f;
  46. Point p2;
  47. p2.x = 300.0f;
  48. p2.y = 100.0f;
  49. Point p3;
  50. p3.x = 500.0f;
  51. p3.y = 1000.0f;
  52. Point p4;
  53. p4.x = 700.0f;
  54. p4.y = 100.0f;
  55. Point p5;
  56. p5.x = 900.0f;
  57. p5.y = 1000.0f;
  58.  
  59. drawBezier(p1, p2, p3, window);
  60. drawBezier(p3, p4, p5, window);
  61.  
  62. window.display();
  63.  
  64. bool beenden = false;
  65. while (!beenden)
  66. {
  67.  
  68. // Events
  69. sf::Event e;
  70. if (window.pollEvent(e))
  71. {
  72. if (e.type = sf::Event::KeyPressed)
  73. if(e.key.code == sf::Keyboard::LAlt)
  74. beenden = true;
  75. }
  76.  
  77. }
  78.  
  79. return 0;
  80. }
  81.  
  82. void drawBezier(Point p1, Point p2, Point p3, sf::RenderWindow &window)
  83. {
  84. std::list<Point> p;
  85.  
  86. for (float t = 0.0f; t < 1; t += 0.01f)
  87. {
  88.  
  89. Point P;
  90. P.x = std::pow(1 - t, 2) * p1.x + (1 - t) * 2 * t * p2.x + t * t * p3.x;
  91.  
  92. P.y = std::pow(1 - t, 2) * p1.y + (1 - t) * 2 * t * p2.y + t * t * p3.y;
  93. p.push_back(P);
  94. }
  95.  
  96. // Iterator
  97. std::list<Point>::iterator it = p.begin();
  98. Point lastPoint = *it;
  99. it++;
  100.  
  101. while(it != p.end())
  102. {
  103. // SFML Graphic
  104. sf::Sprite *S = new sf::Sprite;
  105. sf::IntRect *R = new sf::IntRect;
  106. sf::Texture *T = new sf::Texture;
  107.  
  108. // Winkel zwischen Punkten
  109. float gegenkathete = it->y - lastPoint.y;
  110. float ankathete = it->x - lastPoint.x;
  111. float hypothenuse = std::pow(gegenkathete, 2) + std::pow(ankathete, 2);
  112. float winkel = atan(gegenkathete / ankathete) * 180 / PI;
  113.  
  114. T->loadFromFile("Data/Graphics/dot.png");
  115. R->top = 0.0f;
  116. R->left = 0.0f;
  117. R->width = 2.0f;
  118. R->height = 2.0f;
  119.  
  120. S->setTexture(*T);
  121. S->setTextureRect(*R);
  122. S->setRotation(winkel);
  123. S->setScale(std::pow(hypothenuse, 0.5f) / 2.0f, 1);
  124. //S->setOrigin(R->width / 2, R->height / 2);
  125.  
  126. S->setPosition(it->x, it->y);
  127. lastPoint = *it;
  128. it++;
  129.  
  130. window.draw(*S);
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement