生活随笔
收集整理的這篇文章主要介紹了
qt 绘制五角星图形
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<< qt5.9 c++開發(fā)指南 >>
第8章
三角函數(shù)是基本初等函數(shù)之一,是以角度(數(shù)學(xué)上最常用弧度制,下同)為自變量,角度對(duì)應(yīng)任意角終邊與單位圓交點(diǎn)坐標(biāo)或其比值為因變量的函數(shù)。
角度的弧度制
360 o = 2π rad
180 o = π rad
1 o =(π / 180)rad ≈ 0.01745 rad
1 rad =(180 /π)o ≈ 57.30 o
α 度的角 = α ·(π / 180)rad
代碼:
#include "widget.h"
#include "ui_widget.h"#include <QPainter>void Widget
::paintEvent(QPaintEvent
*event
)
{QPainter
painter(this
);painter
.setRenderHint(QPainter
::Antialiasing
);painter
.setRenderHint(QPainter
::TextAntialiasing
);qreal R
=100; const qreal Pi
=3.14159;qreal deg
=Pi
*72/180;QPoint points
[5]={QPoint(R
,0),QPoint(R
*std
::cos(deg
),-R
*std
::sin(deg
)),QPoint(R
*std
::cos(2*deg
),-R
*std
::sin(2*deg
)),QPoint(R
*std
::cos(3*deg
),-R
*std
::sin(3*deg
)),QPoint(R
*std
::cos(4*deg
),-R
*std
::sin(4*deg
)),};QFont font
;font
.setPointSize(12);font
.setBold(true
);painter
.setFont(font
);QPen penLine
;penLine
.setWidth(2); penLine
.setColor(Qt
::blue
); penLine
.setStyle(Qt
::SolidLine
);penLine
.setCapStyle(Qt
::FlatCap
);penLine
.setJoinStyle(Qt
::BevelJoin
);painter
.setPen(penLine
);QBrush brush
;brush
.setColor(Qt
::yellow
); brush
.setStyle(Qt
::SolidPattern
); painter
.setBrush(brush
);
QPainterPath starPath
;starPath
.moveTo(points
[0]);starPath
.lineTo(points
[2]);starPath
.lineTo(points
[4]);starPath
.lineTo(points
[1]);starPath
.lineTo(points
[3]);starPath
.closeSubpath(); starPath
.addText(points
[0],font
,"0"); starPath
.addText(points
[1],font
,"1");starPath
.addText(points
[2],font
,"2");starPath
.addText(points
[3],font
,"3");starPath
.addText(points
[4],font
,"4");painter
.save(); painter
.translate(100,120);painter
.drawPath(starPath
); painter
.drawText(0,0,"S1");painter
.restore(); painter
.translate(300,120); painter
.scale(0.8,0.8); painter
.rotate(90); painter
.drawPath(starPath
);painter
.drawText(0,0,"S2");painter
.resetTransform(); painter
.translate(500,120); painter
.rotate(-145); painter
.drawPath(starPath
);painter
.drawText(0,0,"S3");
}
Widget
::Widget(QWidget
*parent
) :QWidget(parent
),ui(new Ui
::Widget
)
{ui
->setupUi(this
);setPalette(QPalette(Qt
::white
)); setAutoFillBackground(true
);
resize(600,300);
}Widget
::~Widget()
{delete ui
;
}
總結(jié)
以上是生活随笔為你收集整理的qt 绘制五角星图形的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。