SICP :讨论分层及封装性的又一极好例子。
2023-12-22 16:15:47
.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void Draw_Element(QPainter *p, QPoint point, float height);
void Draw_Level_One(QPainter *p, QPoint point, float height);
void Draw_Level_Two(QPainter *p, QPoint point, float height);
void Draw_Level_Third(QPainter *p, QPoint point, float height);
void Draw_Level_Four(QPainter *p, QPoint point, float height);
void Draw_Level_Five(QPainter *p, QPoint point, float height);
private:
Ui::Widget *ui;
// QWidget interface
protected:
virtual void paintEvent(QPaintEvent *event) override;
};
#endif // WIDGET_H
.cpp文件
#include "widget.h"
#include "ui_widget.h"
#include <QPaintEvent>
#include <QPainter>
#include <QPoint>
#include <QtMath>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::Draw_Element(QPainter* p,QPoint point,float height)
{
// first point
QPoint point_1 = point;
// second point
QPoint point_2 = QPoint(point_1.x() - (2 * height) / qSqrt(3),point_1.y() + height);
// third point
QPoint point_3 = QPoint(point_1.x() + (2 * height) / qSqrt(3),point_1.y() + height);
p->drawLine(point_1,point_2);
p->drawLine(point_2,point_3);
p->drawLine(point_3,point_1);
}
void Widget::Draw_Level_One(QPainter* p,QPoint point,float height)
{
// draw around triangle
Draw_Element(p,point,height);
// draw internal triangle
QPoint point_1 = QPoint(point.x() - height / qSqrt(3),point.y() + height / 2);
QPoint point_2 = QPoint(point.x() + height / qSqrt(3),point.y() + height / 2);
QPoint point_3 = QPoint(point.x(),point.y() + height);
p->drawLine(point_1,point_2);
p->drawLine(point_2,point_3);
p->drawLine(point_3,point_2);
p->drawLine(point_3,point_1);
}
void Widget::Draw_Level_Two(QPainter* p,QPoint point,float height)
{
// draw around triangle
Draw_Element(p,point,height);
// draw internal triangle
QPoint point_1 = QPoint(point.x() - height / qSqrt(3),point.y() + height / 2);
QPoint point_2 = QPoint(point.x() + height / qSqrt(3),point.y() + height / 2);
QPoint point_3 = QPoint(point.x(),point.y() + height);
// first triangle
Draw_Level_One(p,point,height / 2);
// second triangle
Draw_Level_One(p,point_1,height / 2);
// third triangle
Draw_Level_One(p,point_2,height / 2);
}
void Widget::Draw_Level_Third(QPainter* p,QPoint point,float height)
{
// draw around triangle
Draw_Element(p,point,height);
// draw internal triangle
QPoint point_1 = QPoint(point.x() - height / qSqrt(3),point.y() + height / 2);
QPoint point_2 = QPoint(point.x() + height / qSqrt(3),point.y() + height / 2);
QPoint point_3 = QPoint(point.x(),point.y() + height);
// first triangle
Draw_Level_Two(p,point,height / 2);
// second triangle
Draw_Level_Two(p,point_1,height / 2);
// third triangle
Draw_Level_Two(p,point_2,height / 2);
}
void Widget::Draw_Level_Four(QPainter* p,QPoint point,float height)
{
// draw around triangle
Draw_Element(p,point,height);
// draw internal triangle
QPoint point_1 = QPoint(point.x() - height / qSqrt(3),point.y() + height / 2);
QPoint point_2 = QPoint(point.x() + height / qSqrt(3),point.y() + height / 2);
QPoint point_3 = QPoint(point.x(),point.y() + height);
// first triangle
Draw_Level_Third(p,point,height / 2);
// second triangle
Draw_Level_Third(p,point_1,height / 2);
// third triangle
Draw_Level_Third(p,point_2,height / 2);
}
void Widget::Draw_Level_Five(QPainter* p,QPoint point,float height)
{
// draw around triangle
Draw_Element(p,point,height);
// draw internal triangle
QPoint point_1 = QPoint(point.x() - height / qSqrt(3),point.y() + height / 2);
QPoint point_2 = QPoint(point.x() + height / qSqrt(3),point.y() + height / 2);
QPoint point_3 = QPoint(point.x(),point.y() + height);
// first triangle
Draw_Level_Four(p,point,height / 2);
// second triangle
Draw_Level_Four(p,point_1,height / 2);
// third triangle
Draw_Level_Four(p,point_2,height / 2);
}
void Widget::paintEvent(QPaintEvent *event)
{
/*
* left: 10px element 10px level1 10px level2
* */
QPainter painter(this);
painter.setPen(Qt::blue);
painter.setRenderHint(QPainter::Antialiasing, true);
float element_hight = 100;
int init_point_x = 120;
float init_point_y = 40;
// element
Draw_Element(&painter,QPoint(init_point_x,init_point_y),element_hight);
// level 1
init_point_x = 360;
Draw_Level_One(&painter,QPoint(init_point_x,init_point_y),element_hight);
// level 2
init_point_x = 600;
Draw_Level_Two(&painter,QPoint(init_point_x,init_point_y),element_hight);
// level 3
init_point_x = 850;
Draw_Level_Third(&painter,QPoint(init_point_x,init_point_y),element_hight);
// level 4
init_point_x = 1100;
Draw_Level_Four(&painter,QPoint(init_point_x,init_point_y),element_hight);
// level 5
init_point_x = 1350;
Draw_Level_Five(&painter,QPoint(init_point_x,init_point_y),element_hight);
}
文章来源:https://blog.csdn.net/qq_21381465/article/details/135152057
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!