使用 SFML 绘制函数

问题描述 投票:0回答:2

我是 SFML 的新手。我在 Google 上搜索,找到了一种从方程在 SFML 中绘制多个点的方法。例如,我想绘制 200 个点 (x,y),使得 y = 2x,范围为 (-10 < x < 10).

我似乎无法找到正确的函数来在 SFML 中绘制点,因为大多数其他函数只是绘制圆形和其他几何形状。如果有人知道 SFML 中的任何绘图函数,请告诉我(类似这样的:https://www.youtube.com/watch?v=jMrnSa6CHfE&t=42s,不是动画,只是绘图部分)。

非常感谢!

c++ plot sfml
2个回答
1
投票

正如 Galik 所建议的,将像素绘制到图像上是一个很好的解决方案。

您可以尝试以下方法:

sf::Vector2u size;
sf::Image graph;
graph.create(size.x, size.y, sf::Color(255, 255, 255));
// y = 2x
for (unsigned int x = 0; x < size.x; x++)
{
    unsigned int y = 2u * x;
    if (y < size.y)
    {
        graph.setPixel(x, y, sf::Color(0, 0, 0));
    }
}

0
投票

您可以使用VertexArray:

#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Plot");
    sf::Vector2u size = window.getSize();

    sf::VertexArray line(sf::LinesStrip, size.x);
    for (unsigned int x = 0; x < size.x; ++x) {
        unsigned int y = 2 * x;
        if (y < size.y) {
            line[x] = sf::Vertex(sf::Vector2f(x, y), sf::Color::Black);
        }
    }

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        window.clear(sf::Color::White);
        window.draw(line);
        window.display();
    }

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.