1 package tim.prune.data;
4 * Class to hold an altitude and provide conversion functions
8 private boolean _valid = false;
9 private int _value = 0;
10 private int _format = -1;
11 public static final int FORMAT_NONE = -1;
12 public static final int FORMAT_METRES = 0;
13 public static final int FORMAT_FEET = 1;
15 private static final double CONVERT_FEET_TO_METRES = 0.3048;
16 private static final double CONVERT_METRES_TO_FEET = 3.28084;
22 public Altitude(String inString, int inFormat)
24 if (inString != null && !inString.equals(""))
28 _value = Integer.parseInt(inString.trim());
32 catch (NumberFormatException nfe) {}
40 public Altitude(int inValue, int inFormat)
49 * @return true if the value could be parsed
51 public boolean isValid()
58 * @return raw value as int
67 * @return format of number
69 public int getFormat()
76 * Get the altitude value in the specified format
77 * @param inFormat desired format, either FORMAT_METRES or FORMAT_FEET
78 * @return value as an int
80 public int getValue(int inFormat)
82 // TODO: Fix rounding errors here converting between units - return double?
83 if (inFormat == _format)
85 if (inFormat == FORMAT_METRES)
86 return (int) (_value * CONVERT_FEET_TO_METRES);
87 if (inFormat == FORMAT_FEET)
88 return (int) (_value * CONVERT_METRES_TO_FEET);
94 * Interpolate a new Altitude object between the given ones
95 * @param inStart start altitude
96 * @param inEnd end altitude
97 * @param inIndex index of interpolated point
98 * @param inNumSteps number of steps to interpolate
99 * @return Interpolated Altitude object
101 public static Altitude interpolate(Altitude inStart, Altitude inEnd, int inIndex, int inNumSteps)
103 // Check if altitudes are valid
104 if (inStart == null || inEnd == null || !inStart.isValid() || !inEnd.isValid())
105 return new Altitude(null, FORMAT_NONE);
106 // Use altitude format of first point
107 int altFormat = inStart.getFormat();
108 int startValue = inStart.getValue();
109 int endValue = inEnd.getValue(altFormat);
110 int newValue = startValue
111 + (int) ((endValue - startValue) * 1.0 / (inNumSteps + 1) * (inIndex + 1));
112 return new Altitude(newValue, altFormat);