fromPolar(length, angle)函数在官方文档的解释如下:
Returns a QLineF with the given length and angle.
The first point of the line will be on the origin.
Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction. Zero degrees is at the 3 o'clock position.
这个函数返回一个QLineF对象,长度等于length,角度等于angle。这条线的第一个点位于原点。以3点钟方向为angle的零点(也就是X 轴正方向)。逆时针转动,angle变大;顺时针转动,angle变小。
下面通过实例说明:
1)angle = 90
由于Y轴方向指向屏幕下方,所以X轴沿逆时针转动90度后,p2 = (0,-1)
2) angle = 180
X轴正方向旋转180度,末端位于(-1,0)
3) angle = 270
X正半轴逆时针转动270度,末端位于(0,1)
最后附上代码(on_pushButton_2_clicked):
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
double dx1, dy1, dx2, dy2;
dx1 = ui->p1x->value();
dy1 = ui->p1y->value();
dx2 = ui->p2x->value();
dy2 = ui->p2y->value();
QLineF lnef(QPointF(dx1, dy1), QPointF(dx2, dy2));
ui->label->setText(QString().setNum(lnef.angle()));
}
void MainWindow::on_pushButton_2_clicked()
{
double dLen = ui->dBoxLen->value();
double dAngle = ui->dBoxAngle->value();
QLineF lnef = QLineF::fromPolar(dLen, dAngle);
ui->label_3->setText(QString("p1 = %1,%2; p2 = %3,%4").arg(lnef.p1().x()).arg(lnef.p1().y()).arg(lnef.p2().x()).arg(lnef.p2().y()));
}