svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / math / intervals / IntervalUtils.java @ 274
History | View | Annotate | Download (1.38 KB)
1 |
package org.gvsig.math.intervals; |
---|---|
2 |
|
3 |
public class IntervalUtils { |
4 |
|
5 |
/**
|
6 |
* Calculates an nice round interval division. For instance, for
|
7 |
* intervalLenght = 1100000 and numberOfDivisions=5,
|
8 |
* the result would be 250000.
|
9 |
*
|
10 |
* @param intervalLength The full interval to be divided
|
11 |
* @param numberOfDivisions The exact number of divisions to perform
|
12 |
* @return A nice round interval division. The calculated result
|
13 |
* ensures that the whole interval length is covered by the proposed
|
14 |
* division, so it always fulfills the following formula:
|
15 |
* <code>result*numberOfDivisions>=intervalLength</code>
|
16 |
*/
|
17 |
public static double roundIntervalDivision(double intervalLength, int numberOfDivisions) { |
18 |
if (intervalLength<=0.0d || numberOfDivisions<=0) { |
19 |
return 0.0d; |
20 |
} |
21 |
|
22 |
double division = intervalLength/numberOfDivisions;
|
23 |
if (division==0.0d) { |
24 |
return 0.0d; |
25 |
} |
26 |
double digitShift = Math.floor((Math.log10(division))); |
27 |
double scale = Math.pow(10, -digitShift); |
28 |
double firstSignificatDigit = Math.floor(scale*division); |
29 |
double result = firstSignificatDigit*Math.pow(10, digitShift); |
30 |
if (result*numberOfDivisions>=intervalLength) {
|
31 |
return result;
|
32 |
} |
33 |
else {
|
34 |
result = (0.5+firstSignificatDigit)*Math.pow(10, digitShift); |
35 |
if (result*numberOfDivisions>=intervalLength) {
|
36 |
return result;
|
37 |
} |
38 |
} |
39 |
result = (1+firstSignificatDigit)*Math.pow(10, digitShift); |
40 |
return result;
|
41 |
} |
42 |
} |