AI2 Component  (Version nb184)
OrientationSensorUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
15 public class OrientationSensorUtil {
16 
17  private OrientationSensorUtil() {
18  }
19 
31  //VisibleForTesting
32  static float mod(float dividend, float quotient) {
33  float result = dividend % quotient;
34  if (result == 0 || Math.signum(dividend) == Math.signum(quotient)) {
35  return result;
36  } else {
37  return result + quotient;
38  }
39  }
40 
47  public static float normalizeAzimuth(float azimuth) {
48  return mod(azimuth, 360f);
49  }
50 
57  public static float normalizePitch(float pitch) {
58  return mod(pitch + 180f, 360f) - 180f;
59  }
60 
79  public static float normalizeRoll(float roll) {
80  // Guarantee that roll is in [-180, +180]. It could legitimately
81  // be slightly outside due to floating point rounding issues.
82  roll = Math.min(roll, 180f);
83  roll = Math.max(roll, -180f);
84 
85  // If roll is in [-90, +90], we're done.
86  if (roll >= -90 && roll <= 90) {
87  return roll;
88  }
89 
90  // Otherwise, reflect over x-axis to put in 1st or 4th quadrant.
91  roll = 180 - roll;
92 
93  // Put in range [-90, +90].
94  if (roll >= 270) {
95  roll -= 360;
96  }
97  return roll;
98  }
99 }
com.google.appinventor.components.runtime.util.OrientationSensorUtil.normalizeAzimuth
static float normalizeAzimuth(float azimuth)
Definition: OrientationSensorUtil.java:47
com.google.appinventor.components.runtime.util.OrientationSensorUtil.normalizePitch
static float normalizePitch(float pitch)
Definition: OrientationSensorUtil.java:57
com.google.appinventor.components.runtime.util.OrientationSensorUtil
Definition: OrientationSensorUtil.java:15
com.google.appinventor.components.runtime.util.OrientationSensorUtil.normalizeRoll
static float normalizeRoll(float roll)
Definition: OrientationSensorUtil.java:79