Qt drawing COS function

Qt's drawing system is extremely powerful. Let's show the general drawing method of drawing custom graphics.

void MainWindow::paintEvent(QPaintEvent *event)
{
//    paint = new QPainter(this);
//    paint->setPen(QColor(0,0,255));
//    paint->drawLine(30,30,300,300);
//    paint->drawRect(100,100,100,100);
//    paint->setPen((QColor(255,0,0)));
//    paint->drawEllipse(QPoint(300,300),50,50);
   
    QPainter my_paint(this);
    my_paint.setPen(QColor(0,0,255));           //画笔是用来画轮廓的
    my_paint.setBrush(Qt::blue);                     //画刷是用来填充轮廓的
    my_paint.setRenderHint(QPainter::Antialiasing);             //反走样
    my_paint.drawEllipse(QPoint(200,200),100,100);
    
    my_paint.translate(205,0);                          //这相当于把画笔移动到(405,200)这里做画。
    my_paint.setPen(QColor(0,255,0));           //画笔是用来画轮廓的
    my_paint.setBrush(Qt::green);                   //画刷是用来填充轮廓的
    my_paint.drawEllipse(QPoint(200,200),100,100);

    //QPainter什么都能画,无所不能。这也正是Qt绘图系统的强大之处。

    //绘制COS函数图像
    my_paint.translate(-400,50);
    my_paint.setPen(QColor(0,0,255)); 
    double num_x[1000] = {0.0};
    int num_y[1000] = {0};
    double encrease = 0.1;
    for(int i = 0;i < 1000;i++)
    {
        num_x[i] += encrease;
        encrease += 0.1;
    }
    for (int i = 0;i < 1000; i++)
    {
        num_y[i] = 15*cos(num_x[i]);
    }
    for (int i = 0;i < 1000; i++) 
    {
        my_paint.drawPoint(i,400+num_y[i]);
    }
}

The drawing effect is as follows:

The code above shows a rough drawing idea. If you want to draw a very beautiful image, then you need to calculate the relationship between the window size and the drawing point. Only after such careful comparison and calculation can we draw very satisfactory graphics. Of course, this also has a great relationship with the number of samples collected.

 

 

Published 242 original articles · Like 180 · Visits 160,000+

Guess you like

Origin blog.csdn.net/zy010101/article/details/105449397