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 -#include - -static const double Pi = 3.14159265358979323846264338327950288419717; +#include 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 - #include #include #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(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(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 #include - -#define _USE_MATH_DEFINES -#include - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif +#include 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 #include -#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 - +#include 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 #include //============================================================================// @@ -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 +#include #include - -#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 6b18e1ab03..d016f07522 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 e3262c8830..fe1b0425a8 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 746200fd9c..af13b96c32 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 ac410b7415..bb18ba5085 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 ef95b80dd4..75cffe3854 100644 --- a/src/plugins/platforms/android/androidjniinput.cpp +++ b/src/plugins/platforms/android/androidjniinput.cpp @@ -50,6 +50,7 @@ #include #include +#include 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 7e1017426f..299497a35b 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 c2c04b69c5..96d983c8f7 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 53af65e010..097dd111d3 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 46420b49c5..da88a868f3 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 ea08af7608..f2e6973e27 100644 --- a/tests/benchmarks/gui/painting/qpainter/tst_qpainter.cpp +++ b/tests/benchmarks/gui/painting/qpainter/tst_qpainter.cpp @@ -33,10 +33,7 @@ #include #include #include -#include -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif +#include #include @@ -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);