1
Graphics / Solved: How to do the math for a "line" with RectangleShape?
« on: December 04, 2022, 03:08:24 am »
Edit:
I was closer than I thought! I finally figured it out. Here's my code:
-----
I'm trying to follow the advice to use RectangleShape to make lines (with thickness), but I'm stuck on the math. I have the start and end points, but I don't know how to turn that into the length, position, and rotation of the rectangle.
Can anyone help me out? Thanks!
I was closer than I thought! I finally figured it out. Here's my code:
sf::RectangleShape MakeLine(sf::Vector2f StartPoint, sf::Vector2f EndPoint, sf::Color LineColor, float LineThickness)
{
float VectorX = EndPoint.x - StartPoint.x;
float VectorY = EndPoint.y - StartPoint.y;
float Distance = sqrt((VectorX * VectorX) + (VectorY * VectorY));
double Angle = atan2(VectorY, VectorX) * (180 / 3.14159265359);
sf::RectangleShape Line(sf::Vector2f(Distance, LineThickness));
Line.setOrigin(0.0, LineThickness / 2.0);
Line.setPosition(StartPoint);
Line.setRotation(Angle);
Line.setFillColor(LineColor);
return Line;
}
{
float VectorX = EndPoint.x - StartPoint.x;
float VectorY = EndPoint.y - StartPoint.y;
float Distance = sqrt((VectorX * VectorX) + (VectorY * VectorY));
double Angle = atan2(VectorY, VectorX) * (180 / 3.14159265359);
sf::RectangleShape Line(sf::Vector2f(Distance, LineThickness));
Line.setOrigin(0.0, LineThickness / 2.0);
Line.setPosition(StartPoint);
Line.setRotation(Angle);
Line.setFillColor(LineColor);
return Line;
}
-----
I'm trying to follow the advice to use RectangleShape to make lines (with thickness), but I'm stuck on the math. I have the start and end points, but I don't know how to turn that into the length, position, and rotation of the rectangle.
Can anyone help me out? Thanks!