I've been messing around with interpolation and I've almost reached something that I could call usable and then bam big problem. I first experimented by just interpolating between 2 static points and now I added lines that are draw when the mouse moves. The probem is after a couple of seconds the whole thing crashes and I see that .getPosition() returns numbers with 6 or more digits.
Here's the code:
#include <SFML/Graphics.hpp>
#include <iostream>
int setMaxValue(sf::CircleShape circle, bool& y)
{
int a = 0;
if(circle.getPosition().y > circle.getPosition().x)
{
a = circle.getPosition().y;
y = true;
}
else
a = circle.getPosition().x;
return a;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Test");
sf::Event event;
std::vector<sf::CircleShape> circles;
sf::Vector2f mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window))), oldMouseCoords = mouseCoords;
bool ended = false, isY = false;
int maxValue0, maxValue1, maxValue2;
while(window.isOpen())
{
mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window)));
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
{
window.close();
}
if(event.type == sf::Event::MouseMoved)
{
sf::CircleShape newCircle;
newCircle.setRadius(50);
newCircle.setOrigin(newCircle.getRadius(), newCircle.getRadius());
newCircle.setPosition(mouseCoords);
newCircle.setFillColor(sf::Color::Red);
circles.push_back(newCircle);
ended = false;
}
}
if(ended == false && circles.size() > 1)
{
maxValue0 = setMaxValue(circles[circles.size() - 2], isY);
maxValue1 = setMaxValue(circles[circles.size() - 1], isY);
if(maxValue0 > maxValue1)
maxValue2 = maxValue0 - maxValue1;
else
maxValue2 = maxValue1 - maxValue0;
std::cout<<circles.size()<<std::endl;
std::cout<<circles[circles.size() - 2].getPosition().x<<" "<<circles[circles.size() - 2].getPosition().y<<std::endl;
for(int i = 0; i < maxValue2; ++i)
{
sf::CircleShape newCircle;
int x, y;
if(isY == true)
{
y = circles[circles.size() - 2].getPosition().y + i;
x = (((y - circles[circles.size() - 2].getPosition().y) * (circles[circles.size() - 1].getPosition().x - circles[circles.size() - 2].getPosition().x)) / (circles[circles.size() - 1].getPosition().y - circles[circles.size() - 2].getPosition().y)) + circles[circles.size() - 2].getPosition().x;
}
else
{
x = circles[circles.size() - 2].getPosition().x + i;
y = (((x - circles[circles.size() - 1].getPosition().x) * (circles[circles.size() - 1].getPosition().y - circles[circles.size() - 2].getPosition().y)) / (circles[circles.size() - 1].getPosition().x - circles[circles.size() - 2].getPosition().x)) + circles[circles.size() - 2].getPosition().y;
}
sf::Vector2f finalPos(x, y);
newCircle.setRadius(50);
newCircle.setOrigin(newCircle.getRadius(), newCircle.getRadius());
newCircle.setPosition(finalPos);
newCircle.setFillColor(sf::Color::Red);
circles.push_back(newCircle);
if(i == maxValue2 - 1)
{
ended = true;
}
}
}
window.clear(sf::Color::White);
for(auto i : circles)
{
window.draw(i);
}
window.display();
}
}