I am trying to make a function that draws a line from a point to another.
Here is what I have:
const double PI = 3.14159265;
//Line shape
void line(sf::RenderWindow &win, int ax, int ay, int bx, int by) {
//Angle
int xDist = bx - ax;
int yDist = by - ay;
int length = sqrt((xDist * xDist) + (yDist * yDist)); //c = sqrt(a^2 + b^2)
float angle = atan2(yDist, xDist) * 180 / PI; //Get angle and convert into degrees
//Draw a rectangle as the line
sf::RectangleShape line;
line.setSize(sf::Vector2f(length, 3));
line.setOrigin(0, line.getSize().y / 2); //Set the origin of the rectangle at left-middle
line.setRotation(angle); //Rotate the rectangle by the angle calculated
//Draw the rectangle
win.draw(line);
}
And when I call it like so, it works perfectly fine:
line(window, 0, 0, mouseX, mouseY);
Note: mouseX, and mouseY are both defined prior.
But the problem is that when I call it like so, I get the line flipped by 180 degrees:
line(window, mouseX, mouseY, 0, 0);
And if I set the rotation by the angle + 180, it works fine, until I go back to the first method of calling it
(0,0, mouseX, mouseY)
Which now produces the line flipped by 180 degrees.
What exactly is the problem with the algorithm?