diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2017-02-21 17:14:37 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2017-07-05 10:15:34 +0000 |
commit | 8095c33bcddefebd16b7cb08b07690caf877f600 (patch) | |
tree | 816731835b77eb798295c4324f95b23f9916ebef /examples | |
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 <juri.valdmann@qt.io>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'examples')
-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 |
7 files changed, 19 insertions, 40 deletions
diff --git a/examples/dbus/remotecontrolledcar/car/car.cpp b/examples/dbus/remotecontrolledcar/car/car.cpp index 67daac79ba..2de4e6447a 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 7e847c2501..0bd5f44075 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 7a4629c4d2..b7a2d87ada 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 68350f32b7..1ba7dd9ce7 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 1481292e76..851cd17796 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 15f3af77d1..c350f6adee 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 942bbe5ca5..3f85e397ee 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; |