From 993ab0bd1d573f0bbab6e3fcd08d25bc4f32a3d9 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 11 Jan 2017 13:33:05 +0100 Subject: 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 --- examples/sensors/maze/Mouse.qml | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) (limited to 'examples') 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 -- cgit v1.2.1