qt的曲线绘制(QT画跳动的心)
qt的曲线绘制(QT画跳动的心)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#define PI 3.1415926
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
// QObject interface
protected:
void timerEvent(QTimerEvent *event);
// QWidget interface
protected:
void paintEvent(QPaintEvent *event);
private:
int m_nSize = 10;
float m_nT = 0;
};
#endif // MAINWINDOW_H
#include <QPainter>
#include <QPainterPath>
#include <math.h>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
resize(600 600);
startTimer(200);
}
MainWindow::~MainWindow()
{
}
void MainWindow::timerEvent(QTimerEvent *event)
{
if (m_nT > 500)
{
m_nT = 0;
}
if (m_nSize == 5)
{
m_nSize = 10;
}
m_nSize--;
m_nT = 1;
update();
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(this->width() / 2.0 this->height() / 2.0);
painter.setPen(Qt::red);
painter.setBrush(Qt::red);
double t = 0;
double vt = 0.01;
double maxT = 2 * PI;
int maxC = ceil(maxT / vt);
QPainterPath path;
double x = 0;
double y = 0;
QPointF pt(x y);
path.moveTo(pt);
for (int i = 0; i < maxC; i)
{
x = 16 * pow(sin(t) 3);
y = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t);
pt.setX(x * m_nSize);
pt.setY(-y * m_nSize);
t = vt;
path.lineTo(pt);
}
painter.drawPath(path);
}