Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <cmath>
- #include <cstdlib>
- #include <iostream>
- #include <fstream>
- using namespace std;
- const float pi = 3.14159265359;
- const int windowX = 1080;
- const int windowY = 1080;
- sf::Clock clock1;
- class Circle1
- {
- public:
- sf::CircleShape circle;
- float radius;
- sf::Vector2f pos;
- float t = 0; // t is keeping track of the Lerp value between points
- int sign = 1; // can be 1 or -1
- Circle1() // Set default data
- {
- radius = windowX / 25;
- pos = sf::Vector2f(windowX / 2, windowY / 2);
- circle.setFillColor(sf::Color::Green);
- circle.setRadius(radius);
- circle.setOrigin(radius, radius); // Set the origin point to the center of the circle
- circle.setPosition(pos);
- }
- void draw(sf::RenderTarget& w, sf::Vector2f P0, sf::Vector2f P1, sf::Vector2f P2)
- {
- // Formula for tracing the curve by the parameter "t" on both the X- and Y-Axis
- float BtX = (pow(1 - f(t), 2) * P0.x) + 2 * (1 - f(t)) * f(t) * P1.x + pow(f(t), 2) * P2.x;
- float BtY = pow(1 - f(t), 2) * P0.y + 2 * (1 - f(t)) * f(t) * P1.y + pow(f(t), 2) * P2.y;
- pos.x = BtX; // Set the position for the circle
- pos.y = BtY;
- circle.setPosition(pos);
- w.draw(circle);
- if (t >= 1) sign = -1; // 't' must be in range [0, 1]
- if (t <= 0) sign = 1;
- t += 0.0001f * sign; // Can set 0.0001f as a variable (For the slider)
- }
- float f(float t)
- {
- //return t; // Case 1
- //return pow(t, 2); // Case 2
- //return sqrt(t); // Case 3
- //return pow(sin(t * 0.5 * pi), 2); // Case 4
- }
- };
- class ControlPoint
- {
- public:
- sf::CircleShape cPoint;
- float radius;
- sf::Vector2f pos;
- ControlPoint(int posX, int posY) // Set default data
- {
- radius = windowX / 50;
- pos = sf::Vector2f(posX, posY);
- cPoint.setFillColor(sf::Color::Red);
- cPoint.setRadius(radius);
- cPoint.setOrigin(radius, radius); // Set the origin point to the center of the circle
- cPoint.setPosition(pos);
- }
- void draw(sf::RenderTarget& w)
- {
- w.draw(cPoint);
- }
- };
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(windowX, windowY), "Task2");
- Circle1 circle1;
- ControlPoint P0(100, 50); // Create new points
- ControlPoint P1(500, 250);
- ControlPoint P2(50, 500);
- circle1.pos = sf::Vector2f(100, 50);
- circle1.circle.setPosition(circle1.pos);
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- window.clear();
- circle1.draw(window, P0.pos, P1.pos, P2.pos); // Draw all shapes
- P0.draw(window);
- P1.draw(window);
- P2.draw(window);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement