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

Author Topic: How to draw a line between two points ?  (Read 40887 times)

0 Members and 1 Guest are viewing this topic.

Elfayer

  • Newbie
  • *
  • Posts: 42
    • View Profile
How to draw a line between two points ?
« on: April 06, 2013, 03:49:46 pm »
Hi,
I've found in the documentation this part of code :

Quote
sf::VertexArray lines(sf::LinesStrip, 2);
 lines[0].position = sf::Vector2f(10, 0);
 lines[1].position = sf::Vector2f(20, 0);

 window.draw(lines);

(cf. :  http://www.sfml-dev.org/documentation/2.0/classsf_1_1VertexArray.php )

Which is drawing a line ! :)
But, how can I change the color of this line ?

And is there another easier way to draw lines ? (I mean is it the good way to do it ?)

Thanks in advance !

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to draw a line between two points ?
« Reply #1 on: April 06, 2013, 04:04:09 pm »
A VertexArray is an array of Vertex.

If you just want to draw a single line, use a static array of vertex (sf::Vertex[]) rather than a sf::VertexArray, which uses dynamic memory allocation.

This is the best way if you want lines with no thickness. If you want thickness, you must draw a rotated rectangle instead (sf::RectangleShape).
Laurent Gomila - SFML developer

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: How to draw a line between two points ?
« Reply #2 on: April 06, 2013, 06:33:06 pm »
But, how can I change the color of this line ?

Each vertex has a position and a color property.
sf::Vertex line[2];
line[0].position = sf::Vector2f(10, 0);
line[0].color  = sf::Color::Red;
line[1].position = sf::Vector2f(20, 0);
line[1].color = sf::Color::Red;
 

If the two vertices of the line have a different color, you'll obtain a gradient from color 1 to color 2.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to draw a line between two points ?
« Reply #3 on: April 06, 2013, 06:41:07 pm »
When we link to the documentation rather than giving the answer directly, why does someone always come and ruin it? :P
Laurent Gomila - SFML developer

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: How to draw a line between two points ?
« Reply #4 on: April 06, 2013, 08:39:20 pm »
Oh, sorry about that!
I assumed you've only addressed the fact the OP used a vector for just 2 vertices.