尝试使用包含 sf::Quads 的 sf::vertexArray 绘制数学函数图,但我的四边形显示不正确

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

在我的构造函数中我有:

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

和我的图形函数:

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);
    }
}

看起来像这样:Output

你可以看到每个四边形的右侧都有一个三角形切出,而不是看起来像一个普通的正方形。

来自the documentation

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

您的积分排序不满足此要求。

交换前两个坐标即可解决此问题。

或者,交换最后两个坐标也可以解决这个问题。