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/widgets/graphicsview/diagramscene | |
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/widgets/graphicsview/diagramscene')
-rw-r--r-- | examples/widgets/graphicsview/diagramscene/arrow.cpp | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/examples/widgets/graphicsview/diagramscene/arrow.cpp b/examples/widgets/graphicsview/diagramscene/arrow.cpp index 012b9ea2ed..ae7fa7af90 100644 --- a/examples/widgets/graphicsview/diagramscene/arrow.cpp +++ b/examples/widgets/graphicsview/diagramscene/arrow.cpp @@ -51,7 +51,7 @@ #include "arrow.h" -#include <math.h> +#include <cmath> #include <QPen> #include <QPainter> @@ -132,9 +132,7 @@ void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *, setLine(QLineF(intersectPoint, myStartItem->pos())); //! [5] //! [6] - double angle = ::acos(line().dx() / line().length()); - if (line().dy() >= 0) - angle = (Pi * 2) - angle; + double angle = std::atan2(-line().dy(), line().dx()); QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize, cos(angle + Pi / 3) * arrowSize); |