SFML community forums

Help => General => Topic started by: Synyster_Coder on June 04, 2013, 11:28:50 pm

Title: Connecting lines (Vertex Arrays)
Post by: Synyster_Coder on June 04, 2013, 11:28:50 pm
So I have this problem about drawing lines. Basically what I need is to draw a series of lines of equal thickness all connected to each other from where the last left off. Each line will end at a specific point then the next will be drawn to another point, very similar to a line graph, just with thicker lines.

I have got all the lines to connect no problem however I have found that the thickness of the line does not stay the same when the line is at an extreme angle, I have included a screen shot to show what I'm talking about. I'm pretty sure that i just have to position the vertices dependent of the angle but my trigonometry is really bad, can anyone give me a helping hand please, I would be really grateful?

[attachment deleted by admin]
Title: Re: Connecting lines (Vertex Arrays)
Post by: Laurent on June 05, 2013, 07:49:49 am
Your lines look strange. How do you draw them?
Title: Re: Connecting lines (Vertex Arrays)
Post by: Synyster_Coder on June 05, 2013, 10:15:46 am
Well I have my code below: but basically it uses Quads to draw a thick line, quad[0] and quad[3]= starting position, quad[1] and quad[2]= ending position, quad[3] and quad [2] y positions += the line thickness. Then set a texture to it and draw.


#define LINETHICKNESS 32
#define NUMBEROFPOSITIONS 5

mainWindow->clear();

vector<Vector2f> positions;
positions.push_back(sf::Vector2f(startingXPos,startingYPos));
    //fill vector fill of positions to hit
positions.push_back(sf::Vector2f(endXPos,endYPos));

sf::Texture texture1;
texture1.loadFromFile("images.jpg");

sf::VertexArray lines(sf::Quads, 4);
sf::VertexArray lines2(sf::Quads, 4);

for(int i = 1; i < NUMBEROFPOSITIONS; i++)
        {
                lines[0] = positions[i-1];
                lines[3] = positions[i-1];
               
                lines[1] = positions[i];
                lines[2] = positions[i];

                lines[3].position.y += LINETHICKNESS;
                lines[2].position.y += LINETHICKNESS;

                lines[0].texCoords = sf::Vector2f(0,0);
                lines[1].texCoords = sf::Vector2f(400,0);
                lines[2].texCoords = sf::Vector2f(400,100);
                lines[3].texCoords = sf::Vector2f(0,100);
                mainWindow->draw(lines, &texture1);
        }

mainWindow->display();