Created
January 6, 2015 01:08
-
-
Save eddotman/9418c8279cdda13eb185 to your computer and use it in GitHub Desktop.
JS math thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var accelX = Accel.getX //Assuming Accel.getX can read from Pebble or whatever | |
var accelY = Accel.getY | |
var accelZ = Accel.getZ | |
var threshold = 14.5 //total accel bigger than this means seizure | |
//METHOD 1: Checking xyz individually | |
if (accelX > threshold || accelY > threshold || accelZ > threshold ) { | |
console.log('seizure') | |
} | |
//The above will fail in some cases! | |
//Imagine x = 14, y = 14, z = 0. | |
// x,y,z are individually less than threshold (14.5), but the combined | |
// accel is actually sqrt(x^2 + y^2 + z^2) = 19.8 | |
//BETTER METHOD: Use vector magnitude | |
totalAccel = Math.sqrt(Math.pow(accelX, 2) + Math.pow(accelY, 2) + Math.pow(accelZ, 2)) | |
if (totalAccel > threshold) { | |
console.log('seizure!') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment