edu.mines.jtk.util.AxisTics Maven / Gradle / Ivy
Show all versions of edu-mines-jtk Show documentation
/****************************************************************************
Copyright 2004, Colorado School of Mines and others.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
package edu.mines.jtk.util;
import static edu.mines.jtk.util.MathPlus.*;
/**
* Tics for annotating an axis. Given values at the endpoints of the axis,
* axes tics are constructed by computing parameters for both major and
* minor tics. Major tics are a subset of minor tics. Typically, major
* tics are labeled with character strings that represent their values.
*
* Axes tics can be constructed in two ways, by specifying either (1) the
* interval between major tics or (2) the maximum number of major tics.
*
* In the first case, when the major tic interval (a positive number) is
* specified, other tic parameters are easily computed. For example, the
* value of the first major tic equals the smallest multiple of the major
* tic interval that is not less than the minimum of the axis endpoint
* values. Likewise, the number of major tics is computed so that the
* value of the last major tic is not greater than the maximum of the
* axis endpoint values.
*
* In the second case, when the maximum number of major tics is specified,
* the major tic interval is computed to be 2, 5, or 10 times some power
* of 10. Then, other tic parameters are computed as in the first case.
* The tricky part in this second case is choosing the best number from
* the set {2,5,10}. That best number is called the tic multiple,
* and is computed so that the number of major tics is close to, but not
* greater than, the specified maximum number of major tics.
*
* After construction, the counts, increments, and first values of both
* major and minor tics are available.
* @author Dave Hale, Colorado School of Mines
* @version 2004.12.14
*/
public class AxisTics {
/**
* Constructs axis tics for a specified major tic interval.
* @param x1 the value at one end of the axis.
* @param x2 the value at the other end of the axis.
* @param dtic the major tic interval; a positive number.
*/
public AxisTics(double x1, double x2, double dtic) {
double xmin = min(x1,x2);
double xmax = max(x1,x2);
xmin -= (xmax-xmin)*FLT_EPSILON;
xmax += (xmax-xmin)*FLT_EPSILON;
double d = abs(dtic);
double f = ceil(xmin/d)*d;
//double f = ((int)(xmin/d)-1)*d;
//while (f=2)?ntic:2;
double dmax = (xmax-xmin)/(nmax-1);
int nmult = _mult.length;
int nbest = 0;
int mbest = 0;
double dbest = 0.0;
double fbest = 0.0;
for (int imult=0; imultnmax) {
d *= 10;
f = ceil(xmin/d)*d;
n = 1+(int)((xmax-f)/d);
}
if (nbest