Skip to content

Instantly share code, notes, and snippets.

@eddotman
Created January 6, 2015 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddotman/9418c8279cdda13eb185 to your computer and use it in GitHub Desktop.
Save eddotman/9418c8279cdda13eb185 to your computer and use it in GitHub Desktop.
JS math thing
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