例說qt的QLineF::fromPolar()函數

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章