❝520情人节快乐,Qt自绘画心形奉上给大家。 ❞
源码
代码语言:javascript复制#define M_PI 3.14159265358979323846 // pi
void Heart::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.translate(width()/2, height()/2);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(Qt::red));
QVector<QPointF> lines;
int a = 10;
for (float t = 0; t < 2 * M_PI; t = 0.001) {
float x = 16 * pow(sinf(t), 3);
float y = 13 * cosf(t) - 5 * cosf(2 *t) - 2 * cosf(3 * t) - cosf(4 * t);
lines.append(QPointF(x*a, -y*a));
}
painter.drawPolygon(lines);
}