summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2017-01-11 12:33:05 (GMT)
committerEdward Welbourne <edward.welbourne@qt.io>2017-01-16 19:34:05 (GMT)
commit993ab0bd1d573f0bbab6e3fcd08d25bc4f32a3d9 (patch)
treec531adf1f61cba80c35ba0601e91ed3fc41c7b84 /examples
parent19d7ab8d277500e88baf3b23edd62dee4d5d9c46 (diff)
downloadqtsensors-993ab0bd1d573f0bbab6e3fcd08d25bc4f32a3d9.tar.gz
Example: 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: I694fa9e3e2bcdbcf1a4eb4c5d428e2c53ea21732 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/sensors/maze/Mouse.qml16
1 files changed, 1 insertions, 15 deletions
diff --git a/examples/sensors/maze/Mouse.qml b/examples/sensors/maze/Mouse.qml
index 49e2d52..c87ca71 100644
--- a/examples/sensors/maze/Mouse.qml
+++ b/examples/sensors/maze/Mouse.qml
@@ -60,10 +60,6 @@ Item {
visible: true
}
- function distance(origX, origY, newX, newY) {
- return Math.sqrt((Math.pow((newX - origX),2)) + (Math.pow((newY - origY),2)))
- }
-
//Function for moving the mouse
function move(newx, newy)
{
@@ -73,18 +69,8 @@ Item {
//! [0]
var a = newy - mouse.y
var b = newx - mouse.x
- var c = distance(mouse.x, mouse.y, newx, newy)
var radians_to_degrees = 57.2957795
-
- if (a > 0)
- angle = -Math.acos(a / b) * radians_to_degrees
- else
- angle = -Math.asin(b / c) * radians_to_degrees
- if (b > 0)
- angle = -Math.acos(a / c) * radians_to_degrees
- else
- angle = Math.acos(a / c) * radians_to_degrees
-
+ angle = Math.atan2(-b, a) * radians_to_degrees
if (angle < 0)
angle = 360 + angle