Question

So I have been working on a project with a friend but I've hit a dead end with the following code. // The initial angle (for the first vertex) double angle = PI / 2; //double angle=0; int scale = 220; int centerX = 300; int centerY = 250;

// Calculate the location of each vertex
for(int iii = 0; iii < vertices.getVertexCount(); iii++) {
    // Adds the vertices to the diagram
    vertices[iii].position = sf::Vector2f(centerX + cos(angle) * scale, centerY -    sin(angle) * scale);
    vertices[iii].color = sf::Color::White;

    // Calculates the angle that the next point will be at
    angle += q*(TAU / p);
}

// Draws the lines on the diagram
sf::VertexArray lines = sf::VertexArray(sf::Lines, vertices.getVertexCount());
for(int iii = 0; iii < lines.getVertexCount()-1; iii+=2) {
    lines[iii] = vertices[iii];
    lines[iii+1] = vertices[iii+1];
    lines[iii].color = sf::Color(255,(iii)*50,255,255);//sf::Color::White;
    lines[iii+1].color = sf::Color(255,(iii+1)*50,255,255);//sf::Color::White;
}
return lines;

The compiler doesn't give any errors but when I run the code exactly half of the lines show up, but only if p is an even number (p is the number of vertices in the polygon.) For example when I try to draw a square p=4 2 lines will show up if I try to draw a pentagon p=5 all of the lines show up. On a different forum someone suggested adding 0.5f to the coordinates of all vertices to change how openGl rounds. I tried this but it didn't work.

Was it helpful?

Solution

Always provide a screenshot of what you are seeing and a Paint drawing of what you want to achieve

Change

sf::VertexArray lines = sf::VertexArray(sf::Lines, vertices.getVertexCount());
for(int iii = 0; iii < lines.getVertexCount()-1; iii+=2) {
    lines[iii] = vertices[iii];
    lines[iii+1] = vertices[iii+1];
    lines[iii].color = sf::Color(255,(iii)*50,255,255);//sf::Color::White;
    lines[iii+1].color = sf::Color(255,(iii+1)*50,255,255);//sf::Color::White;
}

to

sf::VertexArray lines = sf::VertexArray(sf::LinesStrip, vertices.getVertexCount());
for(int iii = 0; iii < lines.getVertexCount(); iii+=1) {
    lines[iii] = vertices[iii];
    lines[iii].color = sf::Color(255,(iii)*50,255,255);//sf::Color::White;
}

As the primitive type sf::Lines require 2x points than lines you draw. sf::LinesStrip should do the trick in VertexArray.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top