diff options
author | Edward Welbourne <[email protected]> | 2017-02-21 17:14:37 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <[email protected]> | 2017-07-05 10:15:34 +0000 |
commit | 8095c33bcddefebd16b7cb08b07690caf877f600 (patch) | |
tree | 816731835b77eb798295c4324f95b23f9916ebef | |
parent | 827e53540c8de10f3a0b8548523ca80bf9e1c75b (diff) |
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 <[email protected]>
Reviewed-by: Allan Sandfeld Jensen <[email protected]>
Reviewed-by: Marc Mutz <[email protected]>
-rw-r--r-- | examples/dbus/remotecontrolledcar/car/car.cpp | 8 | ||||
-rw-r--r-- | examples/embedded/lightmaps/slippymap.cpp | 14 | ||||
-rw-r--r-- | examples/widgets/animation/stickman/stickman.cpp | 10 | ||||
-rw-r--r-- | examples/widgets/effects/lighting/lighting.cpp | 7 | ||||
-rw-r--r-- | examples/widgets/graphicsview/boxes/glbuffers.cpp | 4 | ||||
-rw-r--r-- | examples/widgets/graphicsview/boxes/trackball.cpp | 8 | ||||
-rw-r--r-- | examples/widgets/itemviews/chart/pieview.cpp | 8 | ||||
-rw-r--r-- | src/gui/math3d/qmatrix4x4.cpp | 6 | ||||
-rw-r--r-- | src/gui/math3d/qquaternion.cpp | 4 | ||||
-rw-r--r-- | src/gui/text/qdistancefield.cpp | 2 | ||||
-rw-r--r-- | src/plugins/generic/tuiotouch/qtuiohandler.cpp | 2 | ||||
-rw-r--r-- | src/plugins/platforms/android/androidjniinput.cpp | 3 | ||||
-rw-r--r-- | src/plugins/platforms/windows/qwindowstabletsupport.cpp | 8 | ||||
-rw-r--r-- | tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp | 4 | ||||
-rw-r--r-- | tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp | 2 | ||||
-rw-r--r-- | tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp | 8 | ||||
-rw-r--r-- | tests/benchmarks/gui/painting/qpainter/tst_qpainter.cpp | 7 |
17 files changed, 39 insertions, 66 deletions
diff --git a/examples/dbus/remotecontrolledcar/car/car.cpp b/examples/dbus/remotecontrolledcar/car/car.cpp index 67daac79ba7..2de4e6447a3 100644 --- a/examples/dbus/remotecontrolledcar/car/car.cpp +++ b/examples/dbus/remotecontrolledcar/car/car.cpp @@ -50,9 +50,7 @@ #include "car.h" #include <QtWidgets/QtWidgets> -#include <math.h> - -static const double Pi = 3.14159265358979323846264338327950288419717; +#include <qmath.h> QRectF Car::boundingRect() const { @@ -135,10 +133,10 @@ void Car::timerEvent(QTimerEvent *event) Q_UNUSED(event); const qreal axelDistance = 54; - qreal wheelsAngleRads = (wheelsAngle * Pi) / 180; + qreal wheelsAngleRads = qDegreesToRadians(wheelsAngle); qreal turnDistance = ::cos(wheelsAngleRads) * axelDistance * 2; qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate - qreal turnRate = (turnRateRads * 180) / Pi; + qreal turnRate = qRadiansToDegrees(turnRateRads); qreal rotation = speed * turnRate; setTransform(QTransform().rotate(rotation), true); diff --git a/examples/embedded/lightmaps/slippymap.cpp b/examples/embedded/lightmaps/slippymap.cpp index 7e847c2501c..0bd5f440751 100644 --- a/examples/embedded/lightmaps/slippymap.cpp +++ b/examples/embedded/lightmaps/slippymap.cpp @@ -48,15 +48,10 @@ ** ****************************************************************************/ -#include <math.h> - #include <QtWidgets> #include <QtNetwork> #include "slippymap.h" - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif +#include "qmath.h" uint qHash(const QPoint& p) { @@ -68,10 +63,10 @@ const int tdim = 256; QPointF tileForCoordinate(qreal lat, qreal lng, int zoom) { + qreal radianLat = qDegreesToRadians(lat); qreal zn = static_cast<qreal>(1 << zoom); qreal tx = (lng + 180.0) / 360.0; - qreal ty = (1.0 - log(tan(lat * M_PI / 180.0) + - 1.0 / cos(lat * M_PI / 180.0)) / M_PI) / 2.0; + qreal ty = 0.5 - log(tan(radianLat) + 1.0 / cos(radianLat)) / M_PI / 2.0; return QPointF(tx * zn, ty * zn); } @@ -86,8 +81,7 @@ qreal latitudeFromTile(qreal ty, int zoom) { qreal zn = static_cast<qreal>(1 << zoom); qreal n = M_PI - 2 * M_PI * ty / zn; - qreal lng = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n))); - return lng; + return qRadiansToDegrees(atan(0.5 * (exp(n) - exp(-n)))); } diff --git a/examples/widgets/animation/stickman/stickman.cpp b/examples/widgets/animation/stickman/stickman.cpp index 7a4629c4d21..b7a2d87adae 100644 --- a/examples/widgets/animation/stickman/stickman.cpp +++ b/examples/widgets/animation/stickman/stickman.cpp @@ -53,13 +53,7 @@ #include <QPainter> #include <QTimer> - -#define _USE_MATH_DEFINES -#include <math.h> - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif +#include <qmath.h> static const qreal Coords[NodeCount * 2] = { 0.0, -150.0, // head, #0 @@ -300,7 +294,7 @@ void StickMan::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge QPointF dist = node2->pos() - node1->pos(); 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(); painter->translate(headPos); diff --git a/examples/widgets/effects/lighting/lighting.cpp b/examples/widgets/effects/lighting/lighting.cpp index 68350f32b74..1ba7dd9ce78 100644 --- a/examples/widgets/effects/lighting/lighting.cpp +++ b/examples/widgets/effects/lighting/lighting.cpp @@ -49,14 +49,9 @@ ****************************************************************************/ #include "lighting.h" - #include <QtWidgets> #include <QtCore/qmath.h> -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - Lighting::Lighting(QWidget *parent): QGraphicsView(parent), angle(0.0) { setScene(&m_scene); @@ -120,7 +115,7 @@ void Lighting::setupScene() void Lighting::animate() { - angle += (M_PI / 30); + angle += qDegreesToRadians(qreal(6)); qreal xs = 200 * qSin(angle) - 40 + 25; qreal ys = 200 * qCos(angle) - 40 + 25; m_lightSource->setPos(xs, ys); diff --git a/examples/widgets/graphicsview/boxes/glbuffers.cpp b/examples/widgets/graphicsview/boxes/glbuffers.cpp index 1481292e762..851cd17796d 100644 --- a/examples/widgets/graphicsview/boxes/glbuffers.cpp +++ b/examples/widgets/graphicsview/boxes/glbuffers.cpp @@ -50,11 +50,11 @@ #include "glbuffers.h" #include <QtGui/qmatrix4x4.h> - +#include <QtCore/qmath.h> 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 xmin = ymin * aspect; const GLdouble xmax = ymax * aspect; diff --git a/examples/widgets/graphicsview/boxes/trackball.cpp b/examples/widgets/graphicsview/boxes/trackball.cpp index 15f3af77d14..c350f6adeea 100644 --- a/examples/widgets/graphicsview/boxes/trackball.cpp +++ b/examples/widgets/graphicsview/boxes/trackball.cpp @@ -50,6 +50,7 @@ #include "trackball.h" #include "scene.h" +#include <qmath.h> #include <cmath> //============================================================================// @@ -101,10 +102,11 @@ void TrackBall::move(const QPointF& p, const QQuaternion &transformation) case Plane: { 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 = 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; case Sphere: @@ -124,7 +126,7 @@ void TrackBall::move(const QPointF& p, const QQuaternion &transformation) currentPos3D.normalize(); 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_axis.normalize(); diff --git a/examples/widgets/itemviews/chart/pieview.cpp b/examples/widgets/itemviews/chart/pieview.cpp index 942bbe5ca57..3f85e397ee5 100644 --- a/examples/widgets/itemviews/chart/pieview.cpp +++ b/examples/widgets/itemviews/chart/pieview.cpp @@ -49,12 +49,8 @@ ****************************************************************************/ #include <QtWidgets> +#include <qmath.h> #include <cmath> - -#ifndef M_PI -#define M_PI 3.1415927 -#endif - #include "pieview.h" PieView::PieView(QWidget *parent) @@ -125,7 +121,7 @@ QModelIndex PieView::indexAt(const QPoint &point) const return QModelIndex(); // 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) angle = 360 + angle; diff --git a/src/gui/math3d/qmatrix4x4.cpp b/src/gui/math3d/qmatrix4x4.cpp index 6b18e1ab03d..d016f07522b 100644 --- a/src/gui/math3d/qmatrix4x4.cpp +++ b/src/gui/math3d/qmatrix4x4.cpp @@ -1128,7 +1128,7 @@ void QMatrix4x4::rotate(float angle, float x, float y, float z) s = 0.0f; c = -1.0f; } else { - float a = angle * M_PI / 180.0f; + float a = qDegreesToRadians(angle); c = std::cos(a); s = std::sin(a); } @@ -1237,7 +1237,7 @@ void QMatrix4x4::projectedRotate(float angle, float x, float y, float z) s = 0.0f; c = -1.0f; } else { - float a = angle * M_PI / 180.0f; + float a = qDegreesToRadians(angle); c = std::cos(a); s = std::sin(a); } @@ -1496,7 +1496,7 @@ void QMatrix4x4::perspective(float verticalAngle, float aspectRatio, float nearP // Construct the projection. QMatrix4x4 m(1); - float radians = (verticalAngle / 2.0f) * M_PI / 180.0f; + float radians = qDegreesToRadians(verticalAngle / 2.0f); float sine = std::sin(radians); if (sine == 0.0f) return; diff --git a/src/gui/math3d/qquaternion.cpp b/src/gui/math3d/qquaternion.cpp index e3262c88302..fe1b0425a85 100644 --- a/src/gui/math3d/qquaternion.cpp +++ b/src/gui/math3d/qquaternion.cpp @@ -408,7 +408,7 @@ QQuaternion QQuaternion::fromAxisAndAngle(const QVector3D& axis, float angle) // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56 // We normalize the result just in case the values are close // 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 c = std::cos(a); QVector3D ax = axis.normalized(); @@ -467,7 +467,7 @@ QQuaternion QQuaternion::fromAxisAndAngle y /= length; z /= length; } - float a = (angle / 2.0f) * M_PI / 180.0f; + float a = qDegreesToRadians(angle / 2.0f); float s = std::sin(a); float c = std::cos(a); return QQuaternion(c, x * s, y * s, z * s).normalized(); diff --git a/src/gui/text/qdistancefield.cpp b/src/gui/text/qdistancefield.cpp index 746200fd9c6..af13b96c32c 100644 --- a/src/gui/text/qdistancefield.cpp +++ b/src/gui/text/qdistancefield.cpp @@ -516,7 +516,7 @@ static void makeDistanceField(QDistanceFieldData *data, const QPainterPath &path for (int i = 0; i < imgWidth * imgHeight; ++i) bits[i] = exteriorColor; - const qreal angleStep = qreal(15 * 3.141592653589793238 / 180); + const qreal angleStep = qDegreesToRadians(qreal(15)); const QPoint rotation(qRound(qCos(angleStep) * 0x4000), qRound(qSin(angleStep) * 0x4000)); // 2:14 signed diff --git a/src/plugins/generic/tuiotouch/qtuiohandler.cpp b/src/plugins/generic/tuiotouch/qtuiohandler.cpp index ac410b74159..bb18ba50855 100644 --- a/src/plugins/generic/tuiotouch/qtuiohandler.cpp +++ b/src/plugins/generic/tuiotouch/qtuiohandler.cpp @@ -523,7 +523,7 @@ QWindowSystemInterface::TouchPoint QTuioHandler::tokenToTouchPoint(const QTuioTo QPointF delta = relPos - relPos.toPoint(); tp.area.moveCenter(win->mapToGlobal(relPos.toPoint()) + delta); 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; } diff --git a/src/plugins/platforms/android/androidjniinput.cpp b/src/plugins/platforms/android/androidjniinput.cpp index ef95b80dd44..75cffe38546 100644 --- a/src/plugins/platforms/android/androidjniinput.cpp +++ b/src/plugins/platforms/android/androidjniinput.cpp @@ -50,6 +50,7 @@ #include <QGuiApplication> #include <QDebug> +#include <QtMath> QT_BEGIN_NAMESPACE @@ -251,7 +252,7 @@ namespace QtAndroidInput QWindowSystemInterface::TouchPoint touchPoint; touchPoint.id = id; touchPoint.pressure = pressure; - touchPoint.rotation = rotation * 180 / M_PI; + touchPoint.rotation = qRadiansToDegrees(rotation); touchPoint.normalPosition = QPointF(double(x / dw), double(y / dh)); touchPoint.state = state; touchPoint.area = QRectF(x - double(minor), diff --git a/src/plugins/platforms/windows/qwindowstabletsupport.cpp b/src/plugins/platforms/windows/qwindowstabletsupport.cpp index 7e1017426fe..299497a35be 100644 --- a/src/plugins/platforms/windows/qwindowstabletsupport.cpp +++ b/src/plugins/platforms/windows/qwindowstabletsupport.cpp @@ -463,13 +463,13 @@ bool QWindowsTabletSupport::translateTabletPacketEvent() // Z = sin(altitude) // X Tilt = arctan(X / Z) // Y Tilt = arctan(Y / Z) - const double radAzim = (packet.pkOrientation.orAzimuth / 10.0) * (M_PI / 180); - const double tanAlt = std::tan((std::abs(packet.pkOrientation.orAltitude / 10.0)) * (M_PI / 180)); + const double radAzim = qDegreesToRadians(packet.pkOrientation.orAzimuth / 10.0); + const double tanAlt = std::tan(qDegreesToRadians(std::abs(packet.pkOrientation.orAltitude / 10.0))); const double degX = std::atan(std::sin(radAzim) / tanAlt); const double degY = std::atan(std::cos(radAzim) / tanAlt); - tiltX = int(degX * (180 / M_PI)); - tiltY = int(-degY * (180 / M_PI)); + tiltX = int(qRadiansToDegrees(degX)); + tiltY = int(qRadiansToDegrees(-degY)); rotation = 360.0 - (packet.pkOrientation.orTwist / 10.0); if (rotation > 180.0) rotation -= 360.0; diff --git a/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp b/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp index c2c04b69c58..96d983c8f74 100644 --- a/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp +++ b/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp @@ -2278,8 +2278,8 @@ void tst_QMatrixNxN::rotate4x4_data() float y = 2.0f; float z = -6.0f; float angle = -45.0f; - float c = std::cos(angle * M_PI / 180.0f); - float s = std::sin(angle * M_PI / 180.0f); + float c = std::cos(qDegreesToRadians(angle)); + float s = std::sin(qDegreesToRadians(angle)); float len = std::sqrt(x * x + y * y + z * z); float xu = x / len; float yu = y / len; diff --git a/tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp b/tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp index 53af65e010e..097dd111d3e 100644 --- a/tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp +++ b/tests/auto/gui/math3d/qquaternion/tst_qquaternion.cpp @@ -815,7 +815,7 @@ void tst_QQuaternion::fromAxisAndAngle() // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56 // to calculate the answer we expect to get. 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 cos_a = std::cos(a); QQuaternion result(cos_a, diff --git a/tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp b/tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp index 46420b49c57..da88a868f3a 100644 --- a/tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp +++ b/tests/auto/gui/painting/qwmatrix/tst_qwmatrix.cpp @@ -114,12 +114,8 @@ void tst_QWMatrix::mapping_data() << QRect( 0, 0, 30, 40 ) << 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 qreal rad = M_PI * degrees / 180.; + const qreal rad = qDegreesToRadians(degrees); return QMatrix(std::cos(rad), -std::sin(rad), std::sin(rad), std::cos(rad), 0, 0); }; @@ -140,7 +136,7 @@ void tst_QWMatrix::mapping_data() #if 0 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), scale * std::sin(rad), scale * std::cos(rad), 0, 0); }; diff --git a/tests/benchmarks/gui/painting/qpainter/tst_qpainter.cpp b/tests/benchmarks/gui/painting/qpainter/tst_qpainter.cpp index ea08af76088..f2e6973e271 100644 --- a/tests/benchmarks/gui/painting/qpainter/tst_qpainter.cpp +++ b/tests/benchmarks/gui/painting/qpainter/tst_qpainter.cpp @@ -33,10 +33,7 @@ #include <QImage> #include <QPaintEngine> #include <QTileRules> -#include <math.h> -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif +#include <qmath.h> #include <private/qpixmap_raster_p.h> @@ -1232,7 +1229,7 @@ QTransform tst_QPainter::transformForAngle(qreal angle) QTransform rotTrans2; rotTrans2.translate(40, 0); - qreal rad = angle * 2. * M_PI / 360.; + qreal rad = qDegreesToRadians(angle); qreal c = ::cos(rad); qreal s = ::sin(rad); |