Qt 绘制表白爱心【李珣表白爱心】
通过QtPainter进行绘制,具体实现可以参考
B站爱心绘制实现
1. functions.h
#include <QPointF>
#include <QRandomGenerator>
#define IMAGE_ENLARGE 15
// 心形函数
QPointF heart_function(qreal t, qreal shrink_ratio = IMAGE_ENLARGE) {
qreal x = 16 * (sin(t) * sin(t) * sin(t));
qreal y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t));
x *= shrink_ratio;
y *= shrink_ratio;
return QPointF(x, y);
}
// 指数分布, 随机内部扩散点
QPointF scatter_inside(QPointF &point, qreal beta = 0.05) {
qreal ratiox = -log(QRandomGenerator::global()->bounded(1.0)) * beta;
qreal ratioy = -log(QRandomGenerator::global()->bounded(1.0)) * beta;
qreal dx = point.x() * ratiox;
qreal dy = point.y() * ratioy;
return QPointF(point.x() - dx, point.y() - dy);
}
QPointF caculate_position(QPointF &point, qreal ratio = 0.9) {
qreal force = 1 / qPow((point.x() * point.x() + point.y() * point.y()), 0.6);
qreal dx = point.x() * force * ratio + QRandomGenerator::global()->bounded(-1, 1);
qreal dy = point.y() * force * ratio + QRandomGenerator::global()->bounded(-1, 1);
return QPointF(point.x() - dx, point.y() - dy);
}
QPointF shrink_ratio(QPointF &point, qreal ratio = 0.9) {
qreal force = -1 / qPow((point.x() * point.x() + point.y() * point.y()), 0.6);
qreal dx = point.x() * force * ratio;
qreal dy = point.y() * force * ratio;
return QPointF(point.x() - dx, point.y() - dy);
}
qreal curve(qreal p) {
return 2 * (2 * sin(4 * p)) / (2 * M_PI);
}
mainwindow.h
#include <QWidget>
#include <QRandomGenerator>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QWidget {
Q_OBJECT
protected:
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
private:
Ui::MainWindow *ui;
qint64 frame = 0; // 当前帧数
QTimer *timer;
QList<QPointF> m_points; // 原始爱心坐标集合
QList<QPointF> m_edge_diffusion_points; // 边缘扩散效果点坐标集合
QList<QPointF> m_center_diffusion_points; // 中心扩散效果点坐标集合
void initPoints(); // 初始化爱心坐标集合
};
mainwindow.cpp
#include "mainwindow.h"
#include "utils/functions.h"
#include "ui_MainWindow.h"
#include <QPainter>
#include <QPainterPath>
#include <QTimer>
#define HEART_COLOR "#ff2121"
#define POINTS_NUMBER 2000
#define FREQUENCY 50
#define DANCESIZE 50
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
// 背景色黑色
this->setStyleSheet("background-color:black;");
// 设置窗口大小
this->setFixedSize(800, 600);
// 设置窗口标题
this->setWindowTitle("LoveStar");
initPoints();
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, qOverload<>(&MainWindow::update));
timer->start(1000 / FREQUENCY);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
// 画笔设置
painter.setPen(QPen(QColor(HEART_COLOR), 1));
painter.save();
painter.translate(this->width() / 2, this->height() / 2);
// 绘制每一帧数据
// 缩放比例
qreal ratio = DANCESIZE * curve(frame / (double) FREQUENCY * M_PI);
QPolygonF points;
qint32 halo_radius = int(4 + 6 * (1 + curve(frame / (double) FREQUENCY * M_PI)));
qint32 halo_number = int(
3000 + 4000 * abs(curve(frame / (double) FREQUENCY * M_PI) * curve(frame / (double) FREQUENCY * M_PI)));
// 光环
for (int i = 0; i < halo_number; i++) {
qreal t = QRandomGenerator::global()->bounded(2 * M_PI);
QPointF p = heart_function(t, IMAGE_ENLARGE);
QPointF pp = shrink_ratio(p, halo_radius);
pp.setX(pp.x() + QRandomGenerator::global()->bounded(-14, 14));
pp.setY(pp.y() + QRandomGenerator::global()->bounded(-14, 14));
points.append(pp);
}
// 轮廓
for (int i = 0; i < POINTS_NUMBER; i++) {
QPointF p = m_points.at(i);
p = caculate_position(p, ratio);
points.append(p);
}
// 边缘扩散效果
for (int i = 0; i < m_edge_diffusion_points.size(); ++i) {
QPointF p = m_edge_diffusion_points.at(i);
p = caculate_position(p, ratio);
points.append(p);
}
// 中心扩散效果
for (int i = 0; i < m_center_diffusion_points.size(); ++i) {
QPointF p = m_center_diffusion_points.at(i);
p = caculate_position(p, ratio);
points.append(p);
}
painter.drawPoints(points);
painter.restore();
frame++;
}
void MainWindow::resizeEvent(QResizeEvent *event) {
update();
}
void MainWindow::initPoints() {
// 初始化爱心坐标集合
m_points.clear();
m_edge_diffusion_points.clear();
m_center_diffusion_points.clear();
for (int i = 0; i < POINTS_NUMBER; i++) {
// 生成随机坐标
qreal t = QRandomGenerator::global()->bounded(2 * M_PI);
QPointF p = heart_function(t);
m_points.append(p);
}
// 初始化边缘扩散效果点坐标集合
for (int i = 0; i < POINTS_NUMBER; i++) {
QPointF p = m_points.at(i);
for (int j = 0; j < 3; ++j) {
QPointF s = scatter_inside(p, 0.05);
m_edge_diffusion_points.append(s);
}
}
// 初始化中心扩散效果点坐标集合
for (int i = 0; i < POINTS_NUMBER * 2; i++) {
qint32 index = QRandomGenerator::global()->bounded(POINTS_NUMBER);
QPointF p = m_points.at(index);
QPointF s = scatter_inside(p, 0.17);
m_center_diffusion_points.append(s);
}
}
main.cpp
#include <QApplication>
#include <mainwindow.h>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return QApplication::exec();
}
最终效果
文章来源:https://www.toymoban.com/news/detail-546653.html
项目地址(欢迎star)
项目源码文章来源地址https://www.toymoban.com/news/detail-546653.html
到了这里,关于Qt 绘制表白爱心【李珣表白爱心】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!