-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
60 lines (52 loc) · 2.07 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var startDegree, endDegree
function calculateDegree() {
//console.log("Mag:", compassMagnetometer.reading.x, ",", compassMagnetometer.reading.y, ",", compassMagnetometer.reading.z)
//console.log("Accel:", compassAccelerometer.reading.x, ",", compassAccelerometer.reading.y, ",", compassAccelerometer.reading.z)
var accelVec = [compassAccelerometer.reading.x, compassAccelerometer.reading.y, compassAccelerometer.reading.z]
var magEast = crossProduct(
[compassMagnetometer.reading.x, compassMagnetometer.reading.y, compassMagnetometer.reading.z],
accelVec)
var magNorth = crossProduct(accelVec, magEast)
magEast = normVec(magEast)
magNorth = normVec(magNorth)
var deviceHeading = [0., 1., -1.] //This is for portrait orientation on android
deviceHeading = normVec(deviceHeading)
var dotWithEast = dotProduct(deviceHeading, magEast)
var dotWithNorth = dotProduct(deviceHeading, magNorth)
var bearingRad = Math.atan2(dotWithEast, dotWithNorth)
//var bearingDeg = bearingRad * 180. / Math.PI
compass_label.text = bearingRad.toFixed(6)
}
function crossProduct(a, b) {
// Check lengths
if (a.length !== 3 || b.length !== 3) {
return
}
return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
}
function normVec(a) {
var compSq = 0.
for (var i = 0; i < a.length; i++)
compSq += Math.pow(a[i], 2)
var compass_sum = Math.pow(compSq, 0.5)
if (compass_sum === 0.)
return
var out = []
for (var j = 0; j < a.length; j++)
out.push(a[j] / compass_sum)
return out
}
function dotProduct(a, b) {
if (a.length !== b.length)
return
var comp = 0.
for (var i = 0; i < a.length; i++)
comp += a[i] * b[i]
return comp
}
function calculateSweetness() {
var compassDegree = Math.abs(endDegree - startDegree)
var refractivity = 1.0 / Math.sin(Math.PI / 2.0 - compassDegree)
toastDisplayer.show("Degree: " + compassDegree.toFixed(
5) + "\nRefractivity: " + refractivity)
}