Search code examples
c++sfml

trying to graph a mathematical function using a sf::vertexArray that contains sf::Quads, but my quads do not display correctly


In my constructor I have:

function.setPrimitiveType(sf::Quads);
numOfPoints = ((XUpperLimit - XLowerLimit) / .001) * 4;
function.resize(numOfPoints);

and my graph function:

void Window::graphFunction() {
    double YVal;

    for (double XVal = XLowerLimit, index = 0.0; XVal < XUpperLimit; XVal += 0.001, index += 4) {
        YVal = tan(XVal);
        if (YVal > YUpperLimit || YVal < YLowerLimit) {
            continue;
        }

        function[index].position = sf::Vector2f((XOrigin + XVal * 20) - 3.f, (YOrigin - YVal * 20) - 3.f);
        function[index + 1].position = sf::Vector2f((XOrigin + XVal * 20) + 3.f, (YOrigin - YVal * 20) - 3.f);
        function[index + 2].position = sf::Vector2f((XOrigin + XVal * 20) - 3.f, (YOrigin - YVal * 20) + 3.f);
        function[index + 3].position = sf::Vector2f((XOrigin + XVal * 20) + 3.f, (YOrigin - YVal * 20) + 3.f);
    }
}

And it looks like this: Output

You can see each Quad has a triangle cut out of it on the right side instead of it looking like a regular square.


Solution

  • From the documentation:

    The 4 points of each quad must be defined consistently, either in clockwise or counter-clockwise order.

    The ordering of your points do not satisfy this requirement.

    Swapping your first two coordinates would fix this.

    Alternately, swapping the last two coordinates would also fix this.