Commit 993ab0bd authored by Edward Welbourne's avatar Edward Welbourne
Browse files

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: default avatarAlex Blasche <alexander.blasche@qt.io>
Showing with 1 addition and 15 deletions
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment