Advertisement
Guest User

Untitled

a guest
May 27th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. int pythagoreanTheorem(int a, int b)
  6. {
  7.     int c = sqrt((a * a) + (b * b));
  8.  
  9.     return c;
  10. }
  11.  
  12. int main()
  13. {
  14.     sf::RenderWindow window(sf::VideoMode(1920, 1080), "Test");
  15.  
  16.     sf::Event event;
  17.  
  18.     std::vector<sf::CircleShape> circles;
  19.     std::vector<sf::RectangleShape> lines;
  20.  
  21.     sf::Vector2f mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window))), oldMouseCoords = mouseCoords;
  22.  
  23.     while(window.isOpen())
  24.     {
  25.         mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window)));
  26.  
  27.         while(window.pollEvent(event))
  28.         {
  29.             if(event.type == sf::Event::Closed)
  30.             {
  31.                 window.close();
  32.             }
  33.  
  34.             if(event.type == sf::Event::MouseMoved)
  35.             {
  36.                 sf::CircleShape circle;
  37.                 circle.setRadius(20);
  38.                 circle.setPosition(mouseCoords);
  39.                 circle.setOrigin(circle.getRadius(), circle.getRadius());
  40.                 circle.setFillColor(sf::Color::Black);
  41.  
  42.                 circles.push_back(circle);
  43.  
  44.                 if(circles.size() > 1)
  45.                 {
  46.                     sf::RectangleShape line;
  47.  
  48.                     line.setSize(sf::Vector2f(circles[circles.size() - 2].getRadius() * 2, pythagoreanTheorem(circles[circles.size() - 1].getPosition().x - circles[circles.size() - 2].getPosition().x, circles[circles.size() - 1].getPosition().y - circles[circles.size() - 2].getPosition().y)));
  49.                     line.setOrigin(line.getSize().x / 2, 0);
  50.  
  51.                     //calculate Sine
  52.                     double idk = (circles[circles.size() - 1].getPosition().x - circles[circles.size() - 2].getPosition().x) / pythagoreanTheorem(circles[circles.size() - 1].getPosition().x - circles[circles.size() - 2].getPosition().x,
  53.                            circles[circles.size() - 1].getPosition().y - circles[circles.size() - 2].getPosition().y);
  54.  
  55.                     //Sine to degrees
  56.                     line.setRotation(-(asin(idk)  * 180 / 3.14));
  57.                     if(circles[circles.size() - 1].getPosition().y < circles[circles.size() - 2].getPosition().y)
  58.                     {
  59.                         line.setRotation(180 - line.getRotation());
  60.                     }
  61.  
  62.                     line.setPosition(circles[circles.size() - 2].getPosition().x, circles[circles.size() - 2].getPosition().y);
  63.                     line.setFillColor(circles[circles.size() - 2].getFillColor());
  64.  
  65.                     lines.push_back(line);
  66.                 }
  67.             }
  68.         }
  69.         window.clear(sf::Color::White);
  70.  
  71.         for(auto i : circles)
  72.         {
  73.             window.draw(i);
  74.         }
  75.  
  76.         for(auto i : lines)
  77.         {
  78.             window.draw(i);
  79.         }
  80.  
  81.         window.display();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement