首页 > 解决方案 > 时间:2019-05-10 标签:c++sfml如何sierpinski triangle

问题描述

我正在制作一个项目并对这个三角形进行编程。但是我在绘制一个位于向量中的圆时遇到了问题(我假设这是某种带有圆的无限数组,以便从中绘制)。但是,根本没有渲染,我无法解决这个问题。我的代码:

    #include <SFML/Graphics.hpp>
#include <cstdlib> 
#include <ctime>
#include <iostream>
#include <vector>

using namespace std;


int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "World of Fractals");

    //cout << "1-random number = " << rand() % 3 << endl;
    //return 0;

    int rnum;

    for (int i = 1; i <= 100; i++) {
        rnum = rand() % 3;
        
    }

    vector<sf::Shape*> shapes;
    
    
    //sf::Vertex point(sf::Vector2f(10, 10), sf::Color::Green);

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

        window.clear();

        for (auto it = shapes.begin(); it != shapes.end(); it++)
        {
            window.draw(**it);
        }

        

        int x1, y1, x2, y2, x3, y3, xpos, ypos, xn,yn;
        x1 = 150; y1 = 10;
        x2 = 300;
        y2 = 210;
        x3 = 100;
        y3 = 210;

    
    

        
        sf::VertexArray triangle(sf::LinesStrip, 4);    
            

        sf::CircleShape shapeF(10.f);
        shapeF.setFillColor(sf::Color::Green);
        shapeF.move(x1, y1);
        window.draw(shapeF);

        sf::CircleShape shapes(10.f);
        shapes.setFillColor(sf::Color::Green);
        shapes.move(x2, y2);
        window.draw(shapes);

        sf::CircleShape shapeT(10.f);
        shapeT.setFillColor(sf::Color::Green);
        shapeT.move(x3, y3);
        window.draw(shapeT);

        /*sf::CircleShape shapess(5.f);
        shapess.setFillColor(sf::Color::Green);
        shapess.move(140, 210);
        window.draw(shapess);*/

        xpos = 140;
        ypos = 210;

        sf::CircleShape* shape = new sf::CircleShape(80);
        shape->setPosition(xpos, ypos);
        shape->setFillColor(sf::Color::Green);
    

        xn = shapes.getPosition().x;
        yn = shapes.getPosition().y;


        if (rnum == 0) // вверх
        {
            xpos = (x1 + xn) / 2;
            ypos = (y1 + yn) / 2;

            sf::CircleShape* shape = new sf::CircleShape(80);
            shape->setPosition(xpos,ypos);
            shape->setFillColor(sf::Color::Green);
            
            
        }
        if (rnum == 1) //лево
        {
            xpos = (x2 + xn) / 2;
            ypos = (y2 + yn) / 2;

            sf::CircleShape* shape = new sf::CircleShape(80);
            shape->setPosition(xpos, ypos);
            shape->setFillColor(sf::Color::Green);
        }
        if (rnum == 2) //право
        {
            xpos = (x3 + xn) / 2;
            ypos = (y3 + yn) / 2;

            sf::CircleShape* shape = new sf::CircleShape(80);
            shape->setPosition(xpos, ypos);
            shape->setFillColor(sf::Color::Green);
            
        }
        
        

        //window.draw(&point,100, sf::Points);
        //.draw(shape);
        window.display();
    }



    return 0;
}

我将不胜感激,非常有必要让圆圈(点)出现在屏幕上并在几秒钟内制作一个谢尔宾斯基三角形(即,还要添加一个计时器)但我认为我会解决这个问题我自己也许主要是通过绘制并可能计算位置来解决问题,但在这里我认为我做的一切都是正确的

标签: c++sfml

解决方案


推荐阅读