diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2017-01-11 11:35:25 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2017-02-23 16:43:35 +0000 |
commit | 0b822c220997046e09fd9cae4414f35f88a91b21 (patch) | |
tree | 4ad858280f7911c608aa6ea397454714afd4e6fc /examples/touch | |
parent | 89870a35bde2f67f9c371ba145e90b86d3e2dd1b (diff) |
Examples: use std::atan2 for simper angle calculations
Using std::atan2 gets the right answer directly from dy and dx,
without having to fix up quadrant as we needed to with acos (albeit we
have to negate dy in some cases, to match prior sense of angles). In
the process, it avoids explicit division, which would be an error when
the line's length is zero.
Change-Id: Ia2923159d38834e08e6f15cbff6766ed419fa804
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'examples/touch')
-rw-r--r-- | examples/touch/pinchzoom/mouse.cpp | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/examples/touch/pinchzoom/mouse.cpp b/examples/touch/pinchzoom/mouse.cpp index f063e96654..3b8e6b5690 100644 --- a/examples/touch/pinchzoom/mouse.cpp +++ b/examples/touch/pinchzoom/mouse.cpp @@ -54,7 +54,7 @@ #include <QPainter> #include <QStyleOption> -#include <math.h> +#include <cmath> static const double Pi = 3.14159265358979323846264338327950288419717; static double TwoPi = 2.0 * Pi; @@ -139,9 +139,7 @@ void Mouse::timerEvent(QTimerEvent *) //! [5] QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0)); if (lineToCenter.length() > 150) { - qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length()); - if (lineToCenter.dy() < 0) - angleToCenter = TwoPi - angleToCenter; + qreal angleToCenter = std::atan2(lineToCenter.dy(), lineToCenter.dx()); angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2); if (angleToCenter < Pi && angleToCenter > Pi / 4) { @@ -170,9 +168,7 @@ void Mouse::timerEvent(QTimerEvent *) continue; QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0)); - qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length()); - if (lineToMouse.dy() < 0) - angleToMouse = TwoPi - angleToMouse; + qreal angleToMouse = std::atan2(lineToMouse.dy(), lineToMouse.dx()); angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2); if (angleToMouse >= 0 && angleToMouse < Pi / 2) { |