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

Author Topic: [SOLVED] how to render line with SFML?  (Read 3643 times)

0 Members and 1 Guest are viewing this topic.

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
[SOLVED] how to render line with SFML?
« 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.
« Last Edit: December 03, 2015, 09:16:21 am by mkalex777 »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: how to render line with SFML?
« Reply #1 on: December 03, 2015, 03:11:36 am »
Draw a rotated RectangleShape.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: how to render line with SFML?
« Reply #2 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?

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: how to render line with SFML?
« Reply #3 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...
« Last Edit: December 03, 2015, 08:48:31 am by mkalex777 »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: how to render line with SFML?
« Reply #4 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  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*