1 package net.sf.openrocket.unit;
3 import java.util.ArrayList;
5 public class FixedPrecisionUnit extends Unit {
7 private final double precision;
8 private final String formatString;
10 public FixedPrecisionUnit(String unit, double precision) {
11 this(unit, precision, 1.0);
14 public FixedPrecisionUnit(String unit, double precision, double multiplier) {
15 super(multiplier, unit);
17 this.precision = precision;
21 while ((p - Math.floor(p)) > 0.0000001) {
25 formatString = "%." + decimals + "f";
30 public double getNextValue(double value) {
31 return round(value + precision);
35 public double getPreviousValue(double value) {
36 return round(value - precision);
41 public double round(double value) {
42 return Math.rint(value/precision)*precision;
49 public String toString(double value) {
50 return String.format(formatString, this.toUnit(value));
55 // TODO: LOW: This is copied from GeneralUnit, perhaps combine
57 public Tick[] getTicks(double start, double end, double minor, double major) {
59 start = toUnit(start);
61 minor = toUnit(minor);
62 major = toUnit(major);
64 if (minor <= 0 || major <= 0 || major < minor) {
65 throw new IllegalArgumentException("getTicks called with minor="+minor+" major="+major);
68 ArrayList<Tick> ticks = new ArrayList<Tick>();
70 int mod2,mod3,mod4; // Moduli for minor-notable, major-nonnotable, major-notable
73 // Find the smallest possible step size
79 // one is the smallest round-ten that is larger than minor
81 // smallest step is round-five
83 mod2 = 2; // Changed later if clashes with major ticks
86 mod2 = 10; // Changed later if clashes with major ticks
89 // Find step size for major ticks
96 // major step is round-five, major-notable is next round-ten
97 double majorstep = one/2;
98 mod3 = (int)Math.round(majorstep/minstep);
101 // major step is round-ten, major-notable is next round-ten
102 mod3 = (int)Math.round(one/minstep);
105 // Check for clashes between minor-notable and major-nonnotable
108 mod2 = 1; // Every minor tick is notable
110 mod2 = 5; // Every fifth minor tick is notable
114 // Calculate starting position
115 int pos = (int)Math.ceil(start/minstep);
116 // System.out.println("mod2="+mod2+" mod3="+mod3+" mod4="+mod4);
117 while (pos*minstep <= end) {
118 double unitValue = pos*minstep;
119 double value = fromUnit(unitValue);
122 ticks.add(new Tick(value,unitValue,true,true));
123 else if (pos%mod3 == 0)
124 ticks.add(new Tick(value,unitValue,true,false));
125 else if (pos%mod2 == 0)
126 ticks.add(new Tick(value,unitValue,false,true));
128 ticks.add(new Tick(value,unitValue,false,false));
133 return ticks.toArray(new Tick[0]);