圆圈的顶点

VertexArray of circles

本文关键字:顶点      更新时间:2023-10-16

我想知道是否可以在sfml中创建一个圆的顶点。我已经寻找答案,但没有找到任何可以帮助的东西。此外,我不了解SFML文档上的部分,我可以在其中创建自己的实体,我认为这也许是我想做的。

编辑:我想这样做,因为我必须画很多圆圈。

感谢您帮助我

而@nvoigt的答案是正确的,我发现对与矢量合作的实现有用(请参阅http://en.cppreference.com/w/cpp/container/vector详细信息,查找" C 容器",有几种类型的容器可以优化读/写入时间)。

您可能不需要上述用例,但是在以后的实施中可能需要它,并考虑这是良好的编码实践。

#include <SFML/Graphics.hpp>
#include <vector>
int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
        // clear the window with black color
        window.clear(sf::Color::Black);
        // initialize myvector
        std::vector<sf::CircleShape> myvector;
        // add 10 circles
        for (int i = 0; i < 10; i++)
        {
          sf::CircleShape shape(50);
          // draw a circle every 100 pixels
          shape.setPosition(i * 100, 25);
          shape.setFillColor(sf::Color(100, 250, 50));
          // copy shape to vector
          myvector.push_back(shape);
        }
        // iterate through vector
        for (std::vector<sf::CircleShape>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
        {
          // draw all circles
          window.draw(*it);
        }
        window.display();
    }
    return 0;
}

sf::CircleShape已经使用顶点阵列的已经(感谢从sf::Shape继承)。您无需做任何额外的事情。

如果您有很多圆圈,请首先使用sf::CircleShape,并且仅在具有实际用例时进行优化,可以测量解决方案。

此外,我将尝试解释为什么没有cirdles的默认顶点。

通过计算机图形学意识形态(在我们的情况下为SFML)顶点是最小的原始图形原始功能,其功能最少。顶点的经典示例是点,线,三角形,瓜德和多边形。对于您的视频卡来说,前四个非常简单,可以存储和绘制。多边形可以是任何几何图形,但处理更重,这就是为什么在3D grapichs中多边形是三角形的原因。

圆圈更复杂。例如,录像带不知道她需要多少要点才能使您的圆圈足够平滑。因此,正如@nvoigt回答的那样,存在一个由更原始的verticies构建的sf ::圆形。