Use qRadiansToDegrees() and qDegreesToRadians() more widely

Especially in examples, where we should show off our convenience
functions, prefer calling these functions over doing arithmetic with
M_PI (or approximations thereto) and 180 (give or take simple
factors).  This incidentally documents what's going on, just by the
name of the function used (and reveals at least one place where
variables were misnamed; the return from atan is in radians, *not*
degrees).

Task-number: QTBUG-58083
Change-Id: I6e5d66721cafab423378f970af525400423e971e
Reviewed-by: Jüri Valdmann <juri.valdmann@qt.io>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Edward Welbourne 2017-02-21 17:14:37 +01:00 committed by Giuseppe D'Angelo
parent 827e53540c
commit 8095c33bcd
17 changed files with 39 additions and 66 deletions

View File

@ -50,9 +50,7 @@
#include "car.h" #include "car.h"
#include <QtWidgets/QtWidgets> #include <QtWidgets/QtWidgets>
#include <math.h> #include <qmath.h>
static const double Pi = 3.14159265358979323846264338327950288419717;
QRectF Car::boundingRect() const QRectF Car::boundingRect() const
{ {
@ -135,10 +133,10 @@ void Car::timerEvent(QTimerEvent *event)
Q_UNUSED(event); Q_UNUSED(event);
const qreal axelDistance = 54; const qreal axelDistance = 54;
qreal wheelsAngleRads = (wheelsAngle * Pi) / 180; qreal wheelsAngleRads = qDegreesToRadians(wheelsAngle);
qreal turnDistance = ::cos(wheelsAngleRads) * axelDistance * 2; qreal turnDistance = ::cos(wheelsAngleRads) * axelDistance * 2;
qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate
qreal turnRate = (turnRateRads * 180) / Pi; qreal turnRate = qRadiansToDegrees(turnRateRads);
qreal rotation = speed * turnRate; qreal rotation = speed * turnRate;
setTransform(QTransform().rotate(rotation), true); setTransform(QTransform().rotate(rotation), true);

View File

@ -48,15 +48,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <math.h>
#include <QtWidgets> #include <QtWidgets>
#include <QtNetwork> #include <QtNetwork>
#include "slippymap.h" #include "slippymap.h"
#include "qmath.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
uint qHash(const QPoint& p) uint qHash(const QPoint& p)
{ {
@ -68,10 +63,10 @@ const int tdim = 256;
QPointF tileForCoordinate(qreal lat, qreal lng, int zoom) QPointF tileForCoordinate(qreal lat, qreal lng, int zoom)
{ {
qreal radianLat = qDegreesToRadians(lat);
qreal zn = static_cast<qreal>(1 << zoom); qreal zn = static_cast<qreal>(1 << zoom);
qreal tx = (lng + 180.0) / 360.0; qreal tx = (lng + 180.0) / 360.0;
qreal ty = (1.0 - log(tan(lat * M_PI / 180.0) + qreal ty = 0.5 - log(tan(radianLat) + 1.0 / cos(radianLat)) / M_PI / 2.0;
1.0 / cos(lat * M_PI / 180.0)) / M_PI) / 2.0;
return QPointF(tx * zn, ty * zn); return QPointF(tx * zn, ty * zn);
} }
@ -86,8 +81,7 @@ qreal latitudeFromTile(qreal ty, int zoom)
{ {
qreal zn = static_cast<qreal>(1 << zoom); qreal zn = static_cast<qreal>(1 << zoom);
qreal n = M_PI - 2 * M_PI * ty / zn; qreal n = M_PI - 2 * M_PI * ty / zn;
qreal lng = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n))); return qRadiansToDegrees(atan(0.5 * (exp(n) - exp(-n))));
return lng;
} }

View File

@ -53,13 +53,7 @@
#include <QPainter> #include <QPainter>
#include <QTimer> #include <QTimer>
#include <qmath.h>
#define _USE_MATH_DEFINES
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static const qreal Coords[NodeCount * 2] = { static const qreal Coords[NodeCount * 2] = {
0.0, -150.0, // head, #0 0.0, -150.0, // head, #0
@ -300,7 +294,7 @@ void StickMan::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
QPointF dist = node2->pos() - node1->pos(); QPointF dist = node2->pos() - node1->pos();
qreal sinAngle = dist.x() / sqrt(pow(dist.x(), 2) + pow(dist.y(), 2)); qreal sinAngle = dist.x() / sqrt(pow(dist.x(), 2) + pow(dist.y(), 2));
qreal angle = asin(sinAngle) * 180.0 / M_PI; qreal angle = qRadiansToDegrees(asin(sinAngle));
QPointF headPos = node1->pos(); QPointF headPos = node1->pos();
painter->translate(headPos); painter->translate(headPos);

View File

@ -49,14 +49,9 @@
****************************************************************************/ ****************************************************************************/
#include "lighting.h" #include "lighting.h"
#include <QtWidgets> #include <QtWidgets>
#include <QtCore/qmath.h> #include <QtCore/qmath.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
Lighting::Lighting(QWidget *parent): QGraphicsView(parent), angle(0.0) Lighting::Lighting(QWidget *parent): QGraphicsView(parent), angle(0.0)
{ {
setScene(&m_scene); setScene(&m_scene);
@ -120,7 +115,7 @@ void Lighting::setupScene()
void Lighting::animate() void Lighting::animate()
{ {
angle += (M_PI / 30); angle += qDegreesToRadians(qreal(6));
qreal xs = 200 * qSin(angle) - 40 + 25; qreal xs = 200 * qSin(angle) - 40 + 25;
qreal ys = 200 * qCos(angle) - 40 + 25; qreal ys = 200 * qCos(angle) - 40 + 25;
m_lightSource->setPos(xs, ys); m_lightSource->setPos(xs, ys);

View File

@ -50,11 +50,11 @@
#include "glbuffers.h" #include "glbuffers.h"
#include <QtGui/qmatrix4x4.h> #include <QtGui/qmatrix4x4.h>
#include <QtCore/qmath.h>
void qgluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) void qgluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{ {
const GLdouble ymax = zNear * tan(fovy * M_PI / 360.0); const GLdouble ymax = zNear * tan(qDegreesToRadians(fovy) / 2.0);
const GLdouble ymin = -ymax; const GLdouble ymin = -ymax;
const GLdouble xmin = ymin * aspect; const GLdouble xmin = ymin * aspect;
const GLdouble xmax = ymax * aspect; const GLdouble xmax = ymax * aspect;

View File

@ -50,6 +50,7 @@
#include "trackball.h" #include "trackball.h"
#include "scene.h" #include "scene.h"
#include <qmath.h>
#include <cmath> #include <cmath>
//============================================================================// //============================================================================//
@ -101,10 +102,11 @@ void TrackBall::move(const QPointF& p, const QQuaternion &transformation)
case Plane: case Plane:
{ {
QLineF delta(m_lastPos, p); QLineF delta(m_lastPos, p);
m_angularVelocity = 180*delta.length() / (PI*msecs); const float angleDelta = qRadiansToDegrees(float(delta.length()));
m_angularVelocity = angleDelta / msecs;
m_axis = QVector3D(-delta.dy(), delta.dx(), 0.0f).normalized(); m_axis = QVector3D(-delta.dy(), delta.dx(), 0.0f).normalized();
m_axis = transformation.rotatedVector(m_axis); m_axis = transformation.rotatedVector(m_axis);
m_rotation = QQuaternion::fromAxisAndAngle(m_axis, 180 / PI * delta.length()) * m_rotation; m_rotation = QQuaternion::fromAxisAndAngle(m_axis, angleDelta) * m_rotation;
} }
break; break;
case Sphere: case Sphere:
@ -124,7 +126,7 @@ void TrackBall::move(const QPointF& p, const QQuaternion &transformation)
currentPos3D.normalize(); currentPos3D.normalize();
m_axis = QVector3D::crossProduct(lastPos3D, currentPos3D); m_axis = QVector3D::crossProduct(lastPos3D, currentPos3D);
float angle = 180 / PI * std::asin(std::sqrt(QVector3D::dotProduct(m_axis, m_axis))); float angle = qRadiansToDegrees(std::asin(std::sqrt(QVector3D::dotProduct(m_axis, m_axis))));
m_angularVelocity = angle / msecs; m_angularVelocity = angle / msecs;
m_axis.normalize(); m_axis.normalize();

View File

@ -49,12 +49,8 @@
****************************************************************************/ ****************************************************************************/
#include <QtWidgets> #include <QtWidgets>
#include <qmath.h>
#include <cmath> #include <cmath>
#ifndef M_PI
#define M_PI 3.1415927
#endif
#include "pieview.h" #include "pieview.h"
PieView::PieView(QWidget *parent) PieView::PieView(QWidget *parent)
@ -125,7 +121,7 @@ QModelIndex PieView::indexAt(const QPoint &point) const
return QModelIndex(); return QModelIndex();
// Determine the angle of the point. // Determine the angle of the point.
double angle = (180 / M_PI) * std::atan2(cy, cx); double angle = qRadiansToDegrees(std::atan2(cy, cx));
if (angle < 0) if (angle < 0)
angle = 360 + angle; angle = 360 + angle;

View File

@ -1128,7 +1128,7 @@ void QMatrix4x4::rotate(float angle, float x, float y, float z)
s = 0.0f; s = 0.0f;
c = -1.0f; c = -1.0f;
} else { } else {
float a = angle * M_PI / 180.0f; float a = qDegreesToRadians(angle);
c = std::cos(a); c = std::cos(a);
s = std::sin(a); s = std::sin(a);
} }
@ -1237,7 +1237,7 @@ void QMatrix4x4::projectedRotate(float angle, float x, float y, float z)
s = 0.0f; s = 0.0f;
c = -1.0f; c = -1.0f;
} else { } else {
float a = angle * M_PI / 180.0f; float a = qDegreesToRadians(angle);
c = std::cos(a); c = std::cos(a);
s = std::sin(a); s = std::sin(a);
} }
@ -1496,7 +1496,7 @@ void QMatrix4x4::perspective(float verticalAngle, float aspectRatio, float nearP
// Construct the projection. // Construct the projection.
QMatrix4x4 m(1); QMatrix4x4 m(1);
float radians = (verticalAngle / 2.0f) * M_PI / 180.0f; float radians = qDegreesToRadians(verticalAngle / 2.0f);
float sine = std::sin(radians); float sine = std::sin(radians);
if (sine == 0.0f) if (sine == 0.0f)
return; return;

View File

@ -408,7 +408,7 @@ QQuaternion QQuaternion::fromAxisAndAngle(const QVector3D& axis, float angle)
// http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56 // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56
// We normalize the result just in case the values are close // We normalize the result just in case the values are close
// to zero, as suggested in the above FAQ. // to zero, as suggested in the above FAQ.
float a = (angle / 2.0f) * M_PI / 180.0f; float a = qDegreesToRadians(angle / 2.0f);
float s = std::sin(a); float s = std::sin(a);
float c = std::cos(a); float c = std::cos(a);
QVector3D ax = axis.normalized(); QVector3D ax = axis.normalized();
@ -467,7 +467,7 @@ QQuaternion QQuaternion::fromAxisAndAngle
y /= length; y /= length;
z /= length; z /= length;
} }
float a = (angle / 2.0f) * M_PI / 180.0f; float a = qDegreesToRadians(angle / 2.0f);
float s = std::sin(a); float s = std::sin(a);
float c = std::cos(a); float c = std::cos(a);
return QQuaternion(c, x * s, y * s, z * s).normalized(); return QQuaternion(c, x * s, y * s, z * s).normalized();

View File

@ -516,7 +516,7 @@ static void makeDistanceField(QDistanceFieldData *data, const QPainterPath &path
for (int i = 0; i < imgWidth * imgHeight; ++i) for (int i = 0; i < imgWidth * imgHeight; ++i)
bits[i] = exteriorColor; bits[i] = exteriorColor;
const qreal angleStep = qreal(15 * 3.141592653589793238 / 180); const qreal angleStep = qDegreesToRadians(qreal(15));
const QPoint rotation(qRound(qCos(angleStep) * 0x4000), const QPoint rotation(qRound(qCos(angleStep) * 0x4000),
qRound(qSin(angleStep) * 0x4000)); // 2:14 signed qRound(qSin(angleStep) * 0x4000)); // 2:14 signed

View File

@ -523,7 +523,7 @@ QWindowSystemInterface::TouchPoint QTuioHandler::tokenToTouchPoint(const QTuioTo
QPointF delta = relPos - relPos.toPoint(); QPointF delta = relPos - relPos.toPoint();
tp.area.moveCenter(win->mapToGlobal(relPos.toPoint()) + delta); tp.area.moveCenter(win->mapToGlobal(relPos.toPoint()) + delta);
tp.velocity = QVector2D(win->size().width() * tc.vx(), win->size().height() * tc.vy()); tp.velocity = QVector2D(win->size().width() * tc.vx(), win->size().height() * tc.vy());
tp.rotation = tc.angle() * 180.0 / M_PI; // convert radians to degrees tp.rotation = qRadiansToDegrees(tc.angle());
return tp; return tp;
} }

View File

@ -50,6 +50,7 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QDebug> #include <QDebug>
#include <QtMath>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -251,7 +252,7 @@ namespace QtAndroidInput
QWindowSystemInterface::TouchPoint touchPoint; QWindowSystemInterface::TouchPoint touchPoint;
touchPoint.id = id; touchPoint.id = id;
touchPoint.pressure = pressure; touchPoint.pressure = pressure;
touchPoint.rotation = rotation * 180 / M_PI; touchPoint.rotation = qRadiansToDegrees(rotation);
touchPoint.normalPosition = QPointF(double(x / dw), double(y / dh)); touchPoint.normalPosition = QPointF(double(x / dw), double(y / dh));
touchPoint.state = state; touchPoint.state = state;
touchPoint.area = QRectF(x - double(minor), touchPoint.area = QRectF(x - double(minor),

View File

@ -463,13 +463,13 @@ bool QWindowsTabletSupport::translateTabletPacketEvent()
// Z = sin(altitude) // Z = sin(altitude)
// X Tilt = arctan(X / Z) // X Tilt = arctan(X / Z)
// Y Tilt = arctan(Y / Z) // Y Tilt = arctan(Y / Z)
const double radAzim = (packet.pkOrientation.orAzimuth / 10.0) * (M_PI / 180); const double radAzim = qDegreesToRadians(packet.pkOrientation.orAzimuth / 10.0);
const double tanAlt = std::tan((std::abs(packet.pkOrientation.orAltitude / 10.0)) * (M_PI / 180)); const double tanAlt = std::tan(qDegreesToRadians(std::abs(packet.pkOrientation.orAltitude / 10.0)));
const double degX = std::atan(std::sin(radAzim) / tanAlt); const double degX = std::atan(std::sin(radAzim) / tanAlt);
const double degY = std::atan(std::cos(radAzim) / tanAlt); const double degY = std::atan(std::cos(radAzim) / tanAlt);
tiltX = int(degX * (180 / M_PI)); tiltX = int(qRadiansToDegrees(degX));
tiltY = int(-degY * (180 / M_PI)); tiltY = int(qRadiansToDegrees(-degY));
rotation = 360.0 - (packet.pkOrientation.orTwist / 10.0); rotation = 360.0 - (packet.pkOrientation.orTwist / 10.0);
if (rotation > 180.0) if (rotation > 180.0)
rotation -= 360.0; rotation -= 360.0;

View File

@ -2278,8 +2278,8 @@ void tst_QMatrixNxN::rotate4x4_data()
float y = 2.0f; float y = 2.0f;
float z = -6.0f; float z = -6.0f;
float angle = -45.0f; float angle = -45.0f;
float c = std::cos(angle * M_PI / 180.0f); float c = std::cos(qDegreesToRadians(angle));
float s = std::sin(angle * M_PI / 180.0f); float s = std::sin(qDegreesToRadians(angle));
float len = std::sqrt(x * x + y * y + z * z); float len = std::sqrt(x * x + y * y + z * z);
float xu = x / len; float xu = x / len;
float yu = y / len; float yu = y / len;

View File

@ -815,7 +815,7 @@ void tst_QQuaternion::fromAxisAndAngle()
// http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56 // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56
// to calculate the answer we expect to get. // to calculate the answer we expect to get.
QVector3D vector = QVector3D(x1, y1, z1).normalized(); QVector3D vector = QVector3D(x1, y1, z1).normalized();
const float a = (angle * M_PI / 180.0) / 2.0; const float a = qDegreesToRadians(angle) / 2.0;
const float sin_a = std::sin(a); const float sin_a = std::sin(a);
const float cos_a = std::cos(a); const float cos_a = std::cos(a);
QQuaternion result(cos_a, QQuaternion result(cos_a,

View File

@ -114,12 +114,8 @@ void tst_QWMatrix::mapping_data()
<< QRect( 0, 0, 30, 40 ) << QRect( 0, 0, 30, 40 )
<< QPolygon( QRect( -300, -400, 300, 400 ) ); << QPolygon( QRect( -300, -400, 300, 400 ) );
#if defined(Q_OS_WIN) && !defined(M_PI)
#define M_PI 3.14159265897932384626433832795f
#endif
const auto rotate = [](qreal degrees) { const auto rotate = [](qreal degrees) {
const qreal rad = M_PI * degrees / 180.; const qreal rad = qDegreesToRadians(degrees);
return QMatrix(std::cos(rad), -std::sin(rad), return QMatrix(std::cos(rad), -std::sin(rad),
std::sin(rad), std::cos(rad), 0, 0); std::sin(rad), std::cos(rad), 0, 0);
}; };
@ -140,7 +136,7 @@ void tst_QWMatrix::mapping_data()
#if 0 #if 0
const auto rotScale = [](qreal degrees, qreal scale) { const auto rotScale = [](qreal degrees, qreal scale) {
const qreal rad = M_PI * degrees / 180.; const qreal rad = qDegreesToRadians(degrees);
return QMatrix(scale * std::cos(rad), -scale * std::sin(rad), return QMatrix(scale * std::cos(rad), -scale * std::sin(rad),
scale * std::sin(rad), scale * std::cos(rad), 0, 0); scale * std::sin(rad), scale * std::cos(rad), 0, 0);
}; };

View File

@ -33,10 +33,7 @@
#include <QImage> #include <QImage>
#include <QPaintEngine> #include <QPaintEngine>
#include <QTileRules> #include <QTileRules>
#include <math.h> #include <qmath.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <private/qpixmap_raster_p.h> #include <private/qpixmap_raster_p.h>
@ -1232,7 +1229,7 @@ QTransform tst_QPainter::transformForAngle(qreal angle)
QTransform rotTrans2; QTransform rotTrans2;
rotTrans2.translate(40, 0); rotTrans2.translate(40, 0);
qreal rad = angle * 2. * M_PI / 360.; qreal rad = qDegreesToRadians(angle);
qreal c = ::cos(rad); qreal c = ::cos(rad);
qreal s = ::sin(rad); qreal s = ::sin(rad);