So it's been a long time. Not because of how hard I found this to be, but rather because of how long I haven't programmed in. Anyways I've pretty much gotten it working, using asin to calculate the angle instead of atan that you recommended me, but it doesn't matter as all trigonometric functions would lead to the same result. I have a problem tho. When drawing lines in the Y coordinate its very smooth and behaves as it should, but when on the X it gets jittery sometimes individual dots are not connected and overall has too many corners and ridges(see pictures - second one is without the dots being drawn).
Code:
#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>
int pythagoreanTheorem(int a, int b)
{
int c = sqrt((a * a) + (b * b));
return c;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Test");
sf::Event event;
std::vector<sf::CircleShape> circles;
std::vector<sf::RectangleShape> lines;
sf::Vector2f mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window))), oldMouseCoords = mouseCoords;
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 circle;
circle.setRadius(20);
circle.setPosition(mouseCoords);
circle.setOrigin(circle.getRadius(), circle.getRadius());
circle.setFillColor(sf::Color::Black);
circles.push_back(circle);
if(circles.size() > 1)
{
sf::RectangleShape line;
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)));
line.setOrigin(line.getSize().x / 2, 0);
//calculate Sine
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,
circles[circles.size() - 1].getPosition().y - circles[circles.size() - 2].getPosition().y);
//Sine to degrees
line.setRotation(-(asin(idk) * 180 / 3.14));
if(circles[circles.size() - 1].getPosition().y < circles[circles.size() - 2].getPosition().y)
{
line.setRotation(180 - line.getRotation());
}
line.setPosition(circles[circles.size() - 2].getPosition().x, circles[circles.size() - 2].getPosition().y);
line.setFillColor(circles[circles.size() - 2].getFillColor());
lines.push_back(line);
}
}
}
window.clear(sf::Color::White);
for(auto i : circles)
{
window.draw(i);
}
for(auto i : lines)
{
window.draw(i);
}
window.display();
}
}