From 5a8eac17f166da312f264d00dce34e441d69d381 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 11 Jan 2017 12:01:30 +0100 Subject: 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 --- src/shared/qtgradienteditor/qtgradientwidget.cpp | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) (limited to 'src/shared/qtgradienteditor') 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); } } -- cgit v1.2.3