• 用Qt 画一个心形


    MainWindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QTimer>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        void timerEvent(QTimerEvent *);
        void paintEvent(QPaintEvent *event);
    
    private:
        int m_x,m_y;
        int m_k;
        float m_t;
    };
    
    #endif // MAINWINDOW_H
    MainWindow.cpp
    #include "mainwindow.h"
    #include <QDebug>
    #include <math.h>
    #include <QPainter>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        this->setFixedSize(600,600);
        m_k = 10;
        m_t = 0;
        startTimer(1);
    
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    
    void MainWindow::timerEvent(QTimerEvent *)
    {
        qDebug() << "timer event " << m_t;
        if (m_t > 500)
        {
            update();
            m_t = 0;
        }
        m_x = 16 * m_k * sin(m_k*m_t)*sin(m_k*m_t)*sin(m_k*m_t);
        m_y = 13 * m_k * cos(m_k*m_t) - 5 * m_k * cos(2 * m_k * m_t) - 2 * m_k * cos(3 * m_k * m_t) - cos(4 * m_k * m_t);
        m_x += this->width() / 2;
        m_y -= this->height() / 2;
        m_y *= -1;
        update(m_x,m_y,1,1);
    
        m_t += 0.1;
    }
    
    #if 1
    void MainWindow::paintEvent(QPaintEvent *event)
    {
        qDebug() << "paintEvent " << m_x << " " << m_y;
        QPainter painter(this);
        painter.setPen(Qt::red);
        painter.drawPoint(m_x, m_y);
    }
    #else
    void MainWindow::paintEvent(QPaintEvent *event)
    {
       double k = 10;
       QPainter painter(this);
       painter.setRenderHint(QPainter::Antialiasing, true);
       QColor my_color(237, 162, 255, 255);
       QBrush my_brush(my_color);
       painter.setPen(Qt::red);
       /*painter.setBrush(my_brush);*/
       painter.translate(this->width()/2, this->height()/2);
       QPainterPath polygonPath;
       polygonPath.setFillRule(Qt::WindingFill);
       float x = 16 * k * sin(0.0)*sin(0.0)*sin(0.0);
       float y = 13 * k * cos(0.0) - 5 * k*cos(0.0) - 2 * k*cos(0.0) - cos(0.0);
       polygonPath.moveTo(x, -y);
       for (double t = 0.01; t < 100; t += 0.05)
       {
           x = 16 * k * sin(k*t)*sin(k*t)*sin(k*t);
           y = 13 * k * cos(k*t) - 5 * k * cos(2 * k * t) - 2 * k * cos(3 * k * t) - cos(4 * k * t);
           polygonPath.lineTo(x, -y);
    #if 0
           painter.drawPoint(x,-y);
           painter.drawLine(0,0,x,-y);
           painter.drawLine(0,0,x+100,-y);
    #endif
       }
       painter.drawPath(polygonPath);
    }
    #endif
  • 相关阅读:
    Xcode ARC,非ARC混搭
    Xcode GData库解析XML
    NSThread
    自定义UITableViewCell中的button实现视图切换
    UITableView
    iOS事件响应链
    结构体和NSData相互转换
    UIView的transform属性
    javascript垃圾回收机制
    ios8 滚动事件解放了
  • 原文地址:https://www.cnblogs.com/nanqiang/p/11542164.html
Copyright © 2020-2023  润新知