Ignore:
Timestamp:
08/08/07 02:46:26 (5 years ago)
Author:
saua
Message:

Fixes Issue #8

File:
1 edited

Legend:

Unmodified
Added
Removed
  • bbtracker_common/trunk/src/org/bbtracker/UnitConverter.java

    r36 r60  
    2828        public abstract String distanceToString(final double distance); 
    2929 
    30         public abstract ScaleConfiguration getScaleConfiguration(final double availableMeter); 
     30        public abstract ScaleConfiguration getScaleDistance(final double availableMeter); 
     31 
     32        public abstract ScaleConfiguration getScaleElevation(final int min, final int max); 
     33 
     34        public abstract ScaleConfiguration getScaleSpeed(final double maxSpeed); 
    3135 
    3236        /** 
     
    5155        } 
    5256 
     57        protected static ScaleConfiguration getScaleConfiguration(final String unit, final double min, final double max) { 
     58                final double diff = max - min; 
     59                if (diff == 0) { 
     60                        final ScaleConfiguration conf = new ScaleConfiguration(); 
     61                        conf.unit = unit; 
     62                        conf.lengthInSourceUnits = diff; 
     63                        conf.labelLocation = new float[] { (float) min }; 
     64                        conf.labelValue = new float[] { 0.0f }; 
     65                        return conf; 
     66                } 
     67 
     68                final ScaleConfiguration conf = new ScaleConfiguration(); 
     69                conf.unit = unit; 
     70                conf.lengthInSourceUnits = diff; 
     71 
     72                // calculate approximate tick size (find correct power of ten) 
     73                double tickSize; 
     74                // I wanted to use log10 to find the correct magnitude, but that's not available in CLDC 1.1 
     75                tickSize = 1; 
     76                if (tickSize > diff) { 
     77                        while (tickSize > diff) { 
     78                                tickSize /= 10; 
     79                        } 
     80                } else { 
     81                        while (tickSize < diff / 10) { 
     82                                tickSize *= 10; 
     83                        } 
     84                } 
     85 
     86                // adjust to get a reasonable number of ticks (usually between 3 and 6) 
     87                int ticks = (int) (diff / tickSize); 
     88                if (ticks > 5) { 
     89                        tickSize *= 2; 
     90                } 
     91 
     92                // might be one to big, but that's better than one to small 
     93                ticks = (int) (diff / tickSize) + 1; 
     94 
     95                conf.labelLocation = new float[ticks]; 
     96                conf.labelValue = new float[ticks]; 
     97 
     98                // find first tick 
     99                double tMin = (int) (min / tickSize) * tickSize; 
     100                if (tMin < min) { 
     101                        tMin += tickSize; 
     102                } 
     103 
     104                double t = tMin; 
     105                int i = 0; 
     106                do { 
     107                        // adding a multiple results in smaller numerical errors than adding each iteration 
     108                        conf.labelValue[i] = (float) t; 
     109                        conf.labelLocation[i] = (float) ((t - min) / diff); 
     110                        t = tMin + (++i) * tickSize; 
     111                } while (t <= max); 
     112 
     113                if (i < ticks) { 
     114                        conf.labelLocation[i] = Float.NaN; 
     115                        conf.labelValue[i] = Float.NaN; 
     116                } 
     117                return conf; 
     118        } 
     119 
    53120        public static class ScaleConfiguration { 
     121                ScaleConfiguration() { 
     122                } 
     123 
    54124                public String unit; 
    55125 
    56                 public int lengthInUnits; 
     126                /** 
     127                 * The values for the labels. 
     128                 *  
     129                 * Any value might be {@link Float#NaN} to indicate an unused entry (this is used to avoid unnecessary 
     130                 * re-allocation of a smaller array). 
     131                 */ 
     132                public float[] labelValue; 
    57133 
    58                 public double lengthInMeter; 
     134                /** 
     135                 * The relative label positions ranging from 0 (minimum) to 1 (maximum). 
     136                 *  
     137                 * Any value might be {@link Float#NaN} just as in {@link #labelValue}. 
     138                 */ 
     139                public float[] labelLocation; 
     140 
     141                /** 
     142                 * The length in source units (meter for distance and elevation, m/s for speed) 
     143                 */ 
     144                public double lengthInSourceUnits; 
    59145        } 
    60146} 
Note: See TracChangeset for help on using the changeset viewer.