# Qt绘制COS函数

2020/04/11 11:34

Qt的绘图系统是极其强大的。下面来展示一下绘制自定义图形的大致绘图方式。

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]);
}
}

0
0 收藏

0 评论
0 收藏
0