Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML how to draw a line  (Read 2260 times)

0 Members and 1 Guest are viewing this topic.

nameless_987

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
SFML how to draw a line
« on: January 24, 2022, 07:49:27 pm »
Hello, I had a problem to draw a line so I made a function with convex and I thought maybe you coul add this feature, here is the code :
// a = x1 ; b = y1 ; c = x2 ; d = y2 ; e = thickness
void line(float a, float b, float c, float d, float e){
    ConvexShape convex;
    convex.setPointCount(4);
    if(c-a == 0){
        convex.setPoint(0, Vector2f(a-e/2, b));
        convex.setPoint(1, Vector2f(a+e/2, b));
        convex.setPoint(2, Vector2f(c+e/2, d));
        convex.setPoint(3, Vector2f(c-e/2, d));
    }
    else{
        float alpha = atan((d-b)/(c-a));
        convex.setPoint(0, Vector2f(a-(e/2)*sin(alpha), b+(e/2)*cos(alpha)));
        convex.setPoint(1, Vector2f(a+(e/2)*sin(alpha), b-(e/2)*cos(alpha)));
        convex.setPoint(2, Vector2f(c+(e/2)*sin(alpha), d-(e/2)*cos(alpha)));
        convex.setPoint(3, Vector2f(c-(e/2)*sin(alpha), d+(e/2)*cos(alpha)));
    }
   
    window.draw(convex);
}
« Last Edit: January 25, 2022, 08:06:58 am by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML how to draw a line
« Reply #1 on: January 27, 2022, 10:17:27 pm »
I presume this is a line with thickness.

As mentioned in the SFML tutorials, a line is just two vertices in a vertex array or - if it has thickness - a rectangle.

That said, positioning that rectangle can be a little tricky.
So, if you need a line with thickness, consider using Selba Ward. It is a collection of SFML drawables that can be used/included independently of each other.
It includes a simple Line object that you can use to create lines with (or without) thickness including adding texture to them as well as keeping the interface you are used to with SFML.
It also includes Spline that you can use to create "polylines" - multiple lines connected end-to-end. It can create Bezier curves. Again, this can have thickness or none - including changing thickness along the spline itself. Have a look at Selba Ward's SFML forum thread here and look at some examples ;)

There are other objects there too, ranging from extremely simple (like Line, for example) to extremely complex (for example, Console Screen).
View Selba Ward's wiki to see the full list. Pick and choose what you like  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*