summaryrefslogtreecommitdiffstats
path: root/src/shared/qtgradienteditor
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2017-01-11 12:01:30 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2017-01-11 17:05:05 +0000
commit5a8eac17f166da312f264d00dce34e441d69d381 (patch)
tree939d30b54af3309ea4c3bc696a738a29c81de9dc /src/shared/qtgradienteditor
parentb103c744aee1f1715ecda6c6041ffc78e8b8ec74 (diff)
Use atan2 for simpler angle calculations
This replaces calls to sqrt, acos and asin (plus some arithmetic and fix-up) with a single call to atan2; it also avoids dividing by a potentially zero length. Change-Id: I829bead54d319c46e5370ef37a3018bc742cdeb4 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Diffstat (limited to 'src/shared/qtgradienteditor')
-rw-r--r--src/shared/qtgradienteditor/qtgradientwidget.cpp22
1 files changed, 2 insertions, 20 deletions
diff --git a/src/shared/qtgradienteditor/qtgradientwidget.cpp b/src/shared/qtgradienteditor/qtgradientwidget.cpp
index 227813d92..f429efea9 100644
--- a/src/shared/qtgradienteditor/qtgradientwidget.cpp
+++ b/src/shared/qtgradienteditor/qtgradientwidget.cpp
@@ -382,15 +382,7 @@ void QtGradientWidget::mousePressEvent(QMouseEvent *e)
y = current.y() - central.y();
x /= size().width() / 2;
y /= size().height() / 2;
- double r = sqrt(x * x + y * y);
-
- double arcSin = asin(y / r);
- double arcCos = acos(x / r);
-
- double angle = arcCos * 180 / M_PI;
- if (arcSin > 0) {
- angle = -angle;
- }
+ double angle = atan2(-y, x) * 180 / M_PI;
d_ptr->m_angleOffset = d_ptr->m_angleConical - angle;
d_ptr->m_dragAngle = d_ptr->m_angleConical;
@@ -480,18 +472,8 @@ void QtGradientWidget::mouseMoveEvent(QMouseEvent *e)
y = current.y() - central.y();
x /= size().width() / 2;
y /= size().height() / 2;
- double r = sqrt(x * x + y * y);
-
- double arcSin = asin(y / r);
- double arcCos = acos(x / r);
-
- double angle = arcCos * 180 / M_PI;
- if (arcSin > 0) {
- angle = -angle;
- }
-
- angle += d_ptr->m_angleOffset;
+ double angle = atan2(-y, x) * 180 / M_PI + d_ptr->m_angleOffset;
d_ptr->setAngleConical(angle);
}
}