00001
00002
00003
00004
00005
00006 namespace NewGamePhysics.Utilities
00007 {
00008 using System;
00009
00013 public class Autorange
00014 {
00023 public static double GetSnapValue(double valueToSnap, bool snapIsGreater)
00024 {
00025
00026 double[] factorsP = { 1.0, 2.0, 5.0, 10.0 };
00027
00028
00029 double[] factorsN = { 10.0, 5.0, 2.0, 1.0 };
00030
00031
00032 double[] factors;
00033 if (valueToSnap != 0.0)
00034 {
00035 bool isPositive = (valueToSnap > 0.0);
00036 double exponent = Math.Floor(Math.Log10(Math.Abs(valueToSnap)));
00037 double valueBase = Math.Pow(10, exponent);
00038
00039 if (isPositive)
00040 {
00041 factors = factorsP;
00042 }
00043 else
00044 {
00045 factors = factorsN;
00046 valueBase = -valueBase;
00047 }
00048
00049 factors[0] *= valueBase;
00050 for (int i = 1; i < 4; i++)
00051 {
00052 factors[i] *= valueBase;
00053 if (factors[i] >= valueToSnap)
00054 {
00055 if (snapIsGreater)
00056 {
00057 return factors[i];
00058 }
00059 else
00060 {
00061 return factors[i - 1];
00062 }
00063 }
00064 }
00065 }
00066
00067
00068 return 0.0;
00069 }
00070 }
00071 }