Advertisement
nobleskyfighter

Untitled

Mar 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. const float pi = 3.14159265359;
  10.  
  11. const int windowX = 1080;
  12. const int windowY = 1080;
  13.  
  14. sf::Clock clock1;
  15.  
  16. class Circle1
  17. {
  18. public:
  19. sf::CircleShape circle;
  20.  
  21. float radius;
  22. sf::Vector2f pos;
  23.  
  24. float t = 0; // t is keeping track of the Lerp value between points
  25. int sign = 1; // can be 1 or -1
  26.  
  27. Circle1() // Set default data
  28. {
  29. radius = windowX / 25;
  30. pos = sf::Vector2f(windowX / 2, windowY / 2);
  31.  
  32. circle.setFillColor(sf::Color::Green);
  33. circle.setRadius(radius);
  34. circle.setOrigin(radius, radius); // Set the origin point to the center of the circle
  35. circle.setPosition(pos);
  36. }
  37.  
  38. void draw(sf::RenderTarget& w, sf::Vector2f P0, sf::Vector2f P1, sf::Vector2f P2)
  39. {
  40. // Formula for tracing the curve by the parameter "t" on both the X- and Y-Axis
  41. float BtX = (pow(1 - f(t), 2) * P0.x) + 2 * (1 - f(t)) * f(t) * P1.x + pow(f(t), 2) * P2.x;
  42. float BtY = pow(1 - f(t), 2) * P0.y + 2 * (1 - f(t)) * f(t) * P1.y + pow(f(t), 2) * P2.y;
  43.  
  44. pos.x = BtX; // Set the position for the circle
  45. pos.y = BtY;
  46.  
  47. circle.setPosition(pos);
  48.  
  49. w.draw(circle);
  50.  
  51. if (t >= 1) sign = -1; // 't' must be in range [0, 1]
  52. if (t <= 0) sign = 1;
  53.  
  54. t += 0.0001f * sign; // Can set 0.0001f as a variable (For the slider)
  55. }
  56.  
  57. float f(float t)
  58. {
  59. //return t; // Case 1
  60. //return pow(t, 2); // Case 2
  61. //return sqrt(t); // Case 3
  62. //return pow(sin(t * 0.5 * pi), 2); // Case 4
  63. }
  64. };
  65.  
  66. class ControlPoint
  67. {
  68. public:
  69. sf::CircleShape cPoint;
  70.  
  71. float radius;
  72. sf::Vector2f pos;
  73.  
  74. ControlPoint(int posX, int posY) // Set default data
  75. {
  76. radius = windowX / 50;
  77. pos = sf::Vector2f(posX, posY);
  78.  
  79. cPoint.setFillColor(sf::Color::Red);
  80. cPoint.setRadius(radius);
  81. cPoint.setOrigin(radius, radius); // Set the origin point to the center of the circle
  82. cPoint.setPosition(pos);
  83. }
  84.  
  85. void draw(sf::RenderTarget& w)
  86. {
  87. w.draw(cPoint);
  88. }
  89. };
  90.  
  91. int main()
  92. {
  93. sf::RenderWindow window(sf::VideoMode(windowX, windowY), "Task2");
  94.  
  95. Circle1 circle1;
  96.  
  97. ControlPoint P0(100, 50); // Create new points
  98. ControlPoint P1(500, 250);
  99. ControlPoint P2(50, 500);
  100.  
  101. circle1.pos = sf::Vector2f(100, 50);
  102. circle1.circle.setPosition(circle1.pos);
  103.  
  104. while (window.isOpen())
  105. {
  106. sf::Event event;
  107. while (window.pollEvent(event))
  108. {
  109. if (event.type == sf::Event::Closed)
  110. window.close();
  111. }
  112.  
  113. window.clear();
  114.  
  115. circle1.draw(window, P0.pos, P1.pos, P2.pos); // Draw all shapes
  116. P0.draw(window);
  117. P1.draw(window);
  118. P2.draw(window);
  119.  
  120. window.display();
  121. }
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement