#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QPixmap>
#include <QPainter>
class KochSnowflakeWidget : public QWidget {
public:
KochSnowflakeWidget(QWidget *parent = nullptr) : QWidget(parent) {
setFixedSize(400, 400);
}
protected:
void paintEvent(QPaintEvent *event) override {
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QPoint p1(50, 350);
QPoint p2(350, 350);
drawKochLine(painter, p1, p2, 3, 0);
QPoint p3(200, 50);
drawKochLine(painter, p2, p3, 3, 120);
drawKochLine(painter, p3, p1, 3, -120);
}
private:
void drawKochLine(QPainter &painter, const QPoint &p1, const QPoint &p2, int depth, double angle) {
if (depth == 0) {
painter.drawLine(p1, p2);
return;
}
double length = QLineF(p1, p2).length() / 3.0;
double rad = qDegreesToRadians(angle);
QPoint p3 = p1 + length * QPointF(cos(rad), -sin(rad));
QPoint p4 = p3 + length * QPointF(cos(rad - M_PI / 3), -sin(rad - M_PI / 3));
QPoint p5 = p2 - length * QPointF(cos(rad), -sin(rad));
drawKochLine(painter, p1, p3, depth - 1, angle);
drawKochLine(painter, p3, p4, depth - 1, angle - 60);
drawKochLine(painter, p4, p5, depth - 1, angle + 60);
drawKochLine(painter, p5, p2, depth - 1, angle);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
QLabel imageLabel(&window);
QPixmap pixmap(":/koch_snowflake.png");
imageLabel.setPixmap(pixmap);
imageLabel.setGeometry(50, 50, 300, 300);
imageLabel.setScaledContents(true);
KochSnowflakeWidget kochWidget(&window);
kochWidget.move(450, 50);
window.resize(800, 400);
window.show();
return app.exec();
}
一个qt/c++ demo,用于显示 Koch Snowflake (科赫雪花) 分形图
2023-04-24 浏览:
标签ad报错:该广告ID(7)不存在。
- 热门标签
-
- 最新发布
-
- Qt 信号槽与原编译系统(2024-06-04)
- Qt信号槽关联中使用普通变量关联(包含不修改和修改槽内数据两种)、使用引用变量关联对(2023-05-05)
- 一个qt/c++ demo,用于显示 Koch Snowflake (科赫雪花) 分(2023-04-24)
- 一个qt/c++ demo,用于显示 Peano Curve(皮亚诺曲线) 分形图(2023-04-24)
- 一个qt/c++ demo,用于显示 Apollonian Gasket(阿波罗尼恩(2023-04-24)
- 一个qt/c++ demo,用于显示 Terdragon Curve(三头龙曲线)(2023-04-24)
- 一个qt/c++ demo,用于显示 Spiral Fractal(螺旋分形) 分形(2023-04-24)
- 一个qt/c++ demo,用于显示 Sierpinski Tetrahedron((2023-04-24)
- 一个qt/c++ demo,用于显示 Gasket Fractal(镂垫分形) 分(2023-04-24)
- 一个qt/c++ demo,用于显示 Hexagonal Gosper Curve(2023-04-24)
- 半年热点
-
-
一个Qt/C++ Demo,用于实现一个用于显示3D瀑布图的控件效果
浏览: 1097
-
一个qt/c++ demo,用于显示分星图
浏览: 1015