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 if (inFormat == _format)
84 if (inFormat == FORMAT_METRES)
85 return (int) (_value * CONVERT_FEET_TO_METRES);
86 if (inFormat == FORMAT_FEET)
87 return (int) (_value * CONVERT_METRES_TO_FEET);
93 * Interpolate a new Altitude object between the given ones
94 * @param inStart start altitude
95 * @param inEnd end altitude
96 * @param inIndex index of interpolated point
97 * @param inNumSteps number of steps to interpolate
98 * @return Interpolated Altitude object
100 public static Altitude interpolate(Altitude inStart, Altitude inEnd, int inIndex, int inNumSteps)
102 // Check if altitudes are valid
103 if (inStart == null || inEnd == null || !inStart.isValid() || !inEnd.isValid())
104 return new Altitude(null, FORMAT_NONE);
105 // Use altitude format of first point
106 int altFormat = inStart.getFormat();
107 int startValue = inStart.getValue();
108 int endValue = inEnd.getValue(altFormat);
109 int newValue = startValue
110 + (int) ((endValue - startValue) * 1.0 / (inNumSteps + 1) * (inIndex + 1));
111 return new Altitude(newValue, altFormat);