SFML community forums

Help => Graphics => Topic started by: mkalex777 on December 03, 2015, 01:01:13 am

Title: [SOLVED] how to render line with SFML?
Post by: mkalex777 on December 03, 2015, 01:01:13 am
I need to render simple line from point V1 to point V2 with thickness N.
How can I do it with SFML?

I know about Window.Draw with primitive type Line, but it render one pixel line. I need to specify line thickness.
Title: Re: how to render line with SFML?
Post by: zsbzsb on December 03, 2015, 03:11:36 am
Draw a rotated RectangleShape.
Title: Re: how to render line with SFML?
Post by: mkalex777 on December 03, 2015, 07:51:11 am
Can you help me with example. I have two Vector2f for start and end points. How to calculate rectangle position and angle?
Title: Re: how to render line with SFML?
Post by: mkalex777 on December 03, 2015, 08:46:45 am
I got it :)

Code is here:
        public static void DrawLine(this RenderTarget target, Vector2f start, Vector2f end, float thickness, Color color)
        {
            var dv = end - start;
            var dl = (float)Math.Sqrt(dv.X * dv.X + dv.Y * dv.Y);
            var uv = dv / dl;
            var up = new Vector2f(-uv.Y, uv.X);
            var offset = up * (thickness / 2F);
            var array = new[]
            {
                new Vertex(start + offset, color),
                new Vertex(end + offset, color),
                new Vertex(end - offset, color),
                new Vertex(start - offset, color),
            };
            target.Draw(array, PrimitiveType.Quads);
        }
 

So simple primitive and so complicated code with SFML...
Title: Re: how to render line with SFML?
Post by: Hapax on December 03, 2015, 10:19:24 pm
So simple primitive and so complicated code with SFML...
I wouldn't say that the 13 lines of code in that function that you posted could be considered "complicated".

You could always consider using this (https://github.com/Hapaxia/SelbaWard/wiki/Line)  ;)