Skip to main content
标签ad报错:该广告ID(9)不存在。
  主页 > Qt入门

一个qt/c++ demo,用于显示Cantor Set(康托尔集合) 分形图

2023-04-24 浏览:
标签ad报错:该广告ID(7)不存在。

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsLineItem>

// 递归绘制 Cantor Set
void drawCantorSet(QGraphicsScene *scene, qreal x1, qreal x2, qreal y)
{
    if (x2-x1 <= 1.0) {
        // 绘制线段
        scene->addLine(x1, -y, x2, -y);
    } else {
        // 计算左右两个子集的起始和结束点
        qreal third = (x2 - x1) / 3.0;
        qreal left1 = x1;
        qreal left2 = x1 + third;
        qreal right1 = x2 - third;
        qreal right2 = x2;

        // 递归绘制左右两个子集
        drawCantorSet(scene, left1, left2, y - 30);
        drawCantorSet(scene, right1, right2, y - 30);
    }
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QGraphicsScene scene;
    QGraphicsView view(&scene);

    const qreal x1 = 50.0;
    const qreal x2 = 550.0;
    const qreal y = 200.0;
    drawCantorSet(&scene, x1, x2, y);

    view.show();
    return app.exec();
}