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

Author Topic: How to draw a line between two points ?  (Read 44455 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: 32498
    • 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: 32498
    • 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.


Bondrusiek

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: How to draw a line between two points ?
« Reply #5 on: November 14, 2024, 05:42:44 pm »
Hi, I created a function which use Bresenham's line algorithm and help drawing a line between two points:
int drawLine(sf::RenderWindow &window, sf::Vector2i point1, sf::Vector2i point2, int lineWidth, sf::Color lineColor)
{
    int x0 = point1.x;
    int y0 = point1.y;
    int x1 = point2.x;
    int y1 = point2.y;
    int dx = abs(x1 - x0);
    int sx, sy;
    if (x0 < x1) {
        sx = 1;
    }
    else {
        sx = -1;
    }

    int dy = -abs(y1 - y0);
    if (y0 < y1) {
        sy = 1;
    }
    else {
        sy = -1;
    }

    int err = dx + dy;

    while (true) {
        sf::RectangleShape rect(sf::Vector2f(lineWidth, lineWidth));
        rect.setFillColor(lineColor);
        rect.setPosition(x0, y0);
        window.draw(rect);
        if (x0 == x1 && y0 == y1) {
            break;
        }
        int e2 = 2 * err;

        if (e2 >= dy) {
            err += dy;
            x0 += sx;
        }

        if (e2 <= dx) {
            err += dx;
            y0 += sy;
        }
    }
}
 

 

anything