From 0b822c220997046e09fd9cae4414f35f88a91b21 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 11 Jan 2017 11:35:25 +0100 Subject: 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 --- examples/widgets/itemviews/chart/pieview.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/widgets/itemviews') diff --git a/examples/widgets/itemviews/chart/pieview.cpp b/examples/widgets/itemviews/chart/pieview.cpp index fb439fae67..942bbe5ca5 100644 --- a/examples/widgets/itemviews/chart/pieview.cpp +++ b/examples/widgets/itemviews/chart/pieview.cpp @@ -125,9 +125,9 @@ QModelIndex PieView::indexAt(const QPoint &point) const return QModelIndex(); // Determine the angle of the point. - double angle = (180 / M_PI) * std::acos(cx / d); - if (cy < 0) - angle = 360 - angle; + double angle = (180 / M_PI) * std::atan2(cy, cx); + if (angle < 0) + angle = 360 + angle; // Find the relevant slice of the pie. double startAngle = 0.0; -- cgit v1.2.3