JClass DesktopViews 6.3.0 API
Documentation

com.klg.jclass.util
Class JCNumberUtil

java.lang.Object
  |
  +--com.klg.jclass.util.JCNumberUtil
Direct Known Subclasses:
JCChart3dUtil, JCChartUtil

public class JCNumberUtil
extends Object

JCNumberUtil is a collection of static methods and static variables that are used throughout JCChart and JCGauge components.


Field Summary
static char DECIMAL
           
static double DEFAULT_FLOAT
          Default floating point value.
static int DEGREES
          Angle unit enum value indicating angle unit is degrees.
static int GRADS
          Angle unit enum value indicating angle unit is gradians.
static double LOG_TEN_BASE_E
          Precalculated ln(10) is more accurate.
static double M_2PI
          Value of pi multiplied by 2.
static double M_PI
          Value of pi.
static double M_PI_2
          Value of pi divided by 2.
static double M_SQRT2
          Square root of 2.
static int RADIANS
          Angle unit enum value indicating angle unit is radians.
static int XBYX1
          pix_x lies outside x1.
static int XBYX2
          pix_x lies outside x2.
static int XINSIDE
          pix_x lies between (x1, x2).
static int YBYORIGIN
          pix_y lies by origin.
static int YBYTOP
          pix_y lies outside top line.
static int YINSIDE
          pix_y lies within top and bottom line.
 
Constructor Summary
JCNumberUtil()
           
 
Method Summary
static double abs(double a)
          Returns the absolute value of a floating point number.
static int abs(int a)
          Returns the absolute value of an integer number.
static double boundAngle(int unit, double angle)
          Guarantees that an angle value lies within its valid range.
static Color brighter(Color color)
          Calculates the color even when it is saturated (for example, when it is black or white).
static double calcError(int base)
          Calculates an appropriate error based on a precision value.
static double clamp(double x, double lo, double hi)
          Ensures that a value is within a provided range.
static int clamp(int x, int lo, int hi)
          Ensures that a value is within a provided range.
static long clamp(long x, long lo, long hi)
          Ensures that a value is within a provided range.
static boolean colorsNear(Color c1, Color c2)
          Determines whether two colors are close to the same color.
static double convertAngle(int oldUnit, int newUnit, double angle)
          Converts an angle value from one unit to another.
static String convertFromSN(String s)
           
static int countInString(String token, String target)
          Counts the occurrences of String token within the larger target String.
static Color darker(Color color)
          Calculates the color even when it is saturated (for example, when it is black or white).
static double degToRad(double d)
          Converts degrees to radians.
static int distance(int p, int start, int width)
          Calculates the separation between an integer value and the nearest boundary of an integer range.
static int distance(int x1, int y1, int x2, int y2)
          Calculates the distance between two points.
static int distance(int x, int y, Rectangle rect)
          Calculates the distance betwen a point and a rectangle.
static long distTrap(int pix_x, int pix_y, int yorigin, double x1, double x2, double y1, double y2)
          Finds the distance from the passed-in point to the specified trapezoid.
static double fmod(double v1, double v2)
          Returns the floating-point remainder of the division of v1 by v2.
static String format(double v, int ndigit)
          Formats a value for display.
static void fudgeItalicLabel(Font font, Dimension dim)
          Fudges the size of a label with italic fonts by making it a little bigger so that all of the italicized font can be seen.
static char getDecimalPointChar()
          Gets the decimal point char.
static int iceil(double val)
          Takes the ceiling of a floating point value.
static int ifloor(double val)
          Takes the floor of a floating point value.
static String internalFormat(double v, int ndigit)
           
static int intLog10(double a)
          Returns log10 of a value, rounded to the nearest integer value.
static boolean isHTML(String s)
          Deprecated. Use com.klg.jclass.util.swing.TextRenderer.isHTML() instead
static int locateX(double x, double lx, double ux)
          Locates an x value within given x bounds.
static int locateXY(double x, double y, double lx, double ux, double ly, double uy)
          Locates a point within given x and y bounds.
static int locateY(double y, double ly, double uy)
          Locates a y value within given y bounds.
static double log10(double a)
          Java only provides a natural logarithm function, so this method is necessary to perform log10 operations.
static String logFormat(int exponent)
          Formats a value for display on a logarithmic axis.
static double max(double a, double b)
          Returns the maximum of two floating point values.
static double min(double a, double b)
          Returns the minimum of two floating point values.
static int nicePrecision(double range)
          Returns a nice precision value for this range.
static Polygon pointsToPolygon(Point[] points)
          Takes an array of points and converts it into a Polygon object.
static double pow10(int x)
          10x function that makes use of a predefined array (tens_val).
static double precCeil(int prec, double value)
          Takes the ceiling of a number to the specified precision.
static double precCorrect(int prec, double value)
          Rounds a number to the specified precision.
static double precFloor(int prec, double value)
          Truncates a number to the specified precision.
static double radToDeg(double r)
          Converts radians to degrees.
static int tickBasedPrecision(double numsp)
          Returns a precision value based on the numspacing or tickspacing value of an axis.
static void trace()
          Debug method that displays a call stack at a particular point.
static boolean validUnit(int unit)
          Checks a value to see if it is part of the angle unit enum; DEGREES, RADIANS, or GRADS.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

M_PI

public static final double M_PI
Value of pi.

See Also:
Constant Field Values

M_PI_2

public static final double M_PI_2
Value of pi divided by 2.

See Also:
Constant Field Values

M_2PI

public static final double M_2PI
Value of pi multiplied by 2.

See Also:
Constant Field Values

M_SQRT2

public static final double M_SQRT2
Square root of 2.

See Also:
Constant Field Values

DEGREES

public static final int DEGREES
Angle unit enum value indicating angle unit is degrees.

See Also:
Constant Field Values

RADIANS

public static final int RADIANS
Angle unit enum value indicating angle unit is radians.

See Also:
Constant Field Values

GRADS

public static final int GRADS
Angle unit enum value indicating angle unit is gradians.

See Also:
Constant Field Values

DEFAULT_FLOAT

public static final double DEFAULT_FLOAT
Default floating point value.

See Also:
Constant Field Values

LOG_TEN_BASE_E

public static final double LOG_TEN_BASE_E
Precalculated ln(10) is more accurate.

See Also:
Constant Field Values

DECIMAL

public static char DECIMAL

XINSIDE

public static final int XINSIDE
pix_x lies between (x1, x2).

See Also:
Constant Field Values

XBYX1

public static final int XBYX1
pix_x lies outside x1.

See Also:
Constant Field Values

XBYX2

public static final int XBYX2
pix_x lies outside x2.

See Also:
Constant Field Values

YINSIDE

public static final int YINSIDE
pix_y lies within top and bottom line.

See Also:
Constant Field Values

YBYTOP

public static final int YBYTOP
pix_y lies outside top line.

See Also:
Constant Field Values

YBYORIGIN

public static final int YBYORIGIN
pix_y lies by origin.

See Also:
Constant Field Values
Constructor Detail

JCNumberUtil

public JCNumberUtil()
Method Detail

getDecimalPointChar

public static char getDecimalPointChar()
Gets the decimal point char.

Returns:

degToRad

public static final double degToRad(double d)
Converts degrees to radians.

Parameters:
d - angle value in degrees
Returns:
converted value in radians

radToDeg

public static final double radToDeg(double r)
Converts radians to degrees.

Parameters:
r - angle value in radians
Returns:
converted value in degrees

validUnit

public static boolean validUnit(int unit)
Checks a value to see if it is part of the angle unit enum; DEGREES, RADIANS, or GRADS.

Parameters:
unit - enum representing an angle unit
Returns:
true if the unit enum is valid; false otherwise

boundAngle

public static double boundAngle(int unit,
                                double angle)
Guarantees that an angle value lies within its valid range. For example, all degree values should be between -360 and +360.

Parameters:
unit - unit for the angle value
angle - angle value
Returns:

log10

public static double log10(double a)
Java only provides a natural logarithm function, so this method is necessary to perform log10 operations.

Parameters:
a - floating-point value
Returns:
log-base-10 of the provided value

intLog10

public static int intLog10(double a)
Returns log10 of a value, rounded to the nearest integer value. Useful to the axis calculation code.

Parameters:
a -
Returns:

nicePrecision

public static int nicePrecision(double range)
Returns a nice precision value for this range. It does not take into account font size, window size, etc. Instead, it uses log10 of the range.

Parameters:
range -
Returns:

tickBasedPrecision

public static int tickBasedPrecision(double numsp)
Returns a precision value based on the numspacing or tickspacing value of an axis. This method looks for the number of significant (non-zero) digits either after or before a decimal place and uses that as the default precision.

Parameters:
numsp -
Returns:

clamp

public static double clamp(double x,
                           double lo,
                           double hi)
Ensures that a value is within a provided range.

Parameters:
x - value that requires clamping
lo - low value in the range
hi - high value in the range
Returns:
clamped value that lies between lo and hi

clamp

public static int clamp(int x,
                        int lo,
                        int hi)
Ensures that a value is within a provided range.

Parameters:
x - value that requires clamping
lo - low value in the range
hi - high value in the range
Returns:
clamped value that lies between lo and hi

clamp

public static long clamp(long x,
                         long lo,
                         long hi)
Ensures that a value is within a provided range.

Parameters:
x - value that requires clamping
lo - low value in the range
hi - high value in the range
Returns:
clamped value that lies between lo and hi

precFloor

public static double precFloor(int prec,
                               double value)
Truncates a number to the specified precision. If the number is negative, takes the ceiling of its negated value instead.

Parameters:
prec - precision value
value - value to truncate
Returns:
value truncated at the specified precision

precCeil

public static double precCeil(int prec,
                              double value)
Takes the ceiling of a number to the specified precision. If the number is negative, takes the floor of its negated value instead.

Parameters:
prec - precision value
value - value to truncate
Returns:
ceiling of value at the specified precision

logFormat

public static String logFormat(int exponent)
Formats a value for display on a logarithmic axis.
Note: This routine currently calls JCNumberUtil.format().

Parameters:
exponent - exponent of log value
Returns:
formatted String

format

public static String format(double v,
                            int ndigit)
Formats a value for display. Performs the equivalent of sprintf(buffer, "%.*f", ndigit, v);

Parameters:
v - value to format
ndigit - number of digits to appear in the formatted String
Returns:
formatted String

internalFormat

public static String internalFormat(double v,
                                    int ndigit)

convertFromSN

public static String convertFromSN(String s)

convertAngle

public static double convertAngle(int oldUnit,
                                  int newUnit,
                                  double angle)
Converts an angle value from one unit to another. Units may be DEGREES, RADIANS, or GRADS.

Parameters:
oldUnit - unit of the provided angle value
newUnit - unit of the new angle value
angle - angle value
Returns:
converted angle value

pow10

public static double pow10(int x)
10x function that makes use of a predefined array (tens_val).

Parameters:
x - x in 10x
Returns:
requested power of 10

precCorrect

public static double precCorrect(int prec,
                                 double value)
Rounds a number to the specified precision.

Parameters:
prec - requested precision
value - number to be rounded
Returns:
rounded value

max

public static double max(double a,
                         double b)
Returns the maximum of two floating point values. Faster than the naive Math.max().

Parameters:
a - first floating point value
b - second floating point value
Returns:
maximum of a and b

min

public static double min(double a,
                         double b)
Returns the minimum of two floating point values. Faster than the naive Math.min().

Parameters:
a - first floating point value
b - second floating point value
Returns:
minimum of a and b

abs

public static double abs(double a)
Returns the absolute value of a floating point number. Faster than the naive Math.abs().

Parameters:
a - floating point value
Returns:
absolute value of a

abs

public static int abs(int a)
Returns the absolute value of an integer number. Faster than the naive Math.abs().

Parameters:
a - floating point value
Returns:
absolute value of a

fmod

public static double fmod(double v1,
                          double v2)
Returns the floating-point remainder of the division of v1 by v2.

Parameters:
v1 - floating-point dividend
v2 - floating-point divisor
Returns:
floating-point remainder, Integer.MAX_VALUE if the divisor is zero

trace

public static void trace()
Debug method that displays a call stack at a particular point.


distance

public static int distance(int x1,
                           int y1,
                           int x2,
                           int y2)
Calculates the distance between two points.

Parameters:
x1 - first x point
y1 - first y point
x2 - second x point
y2 - second y point
Returns:
distance between the two points

distance

public static int distance(int x,
                           int y,
                           Rectangle rect)
Calculates the distance betwen a point and a rectangle.

Parameters:
x - x point
y - y point
rect - rectangle
Returns:
distance between the point and the rectangle

distance

public static int distance(int p,
                           int start,
                           int width)
Calculates the separation between an integer value and the nearest boundary of an integer range.

Parameters:
p - integer value
start - start point of range
width - end point of range
Returns:
separation between the integer value and the specified range

distTrap

public static long distTrap(int pix_x,
                            int pix_y,
                            int yorigin,
                            double x1,
                            double x2,
                            double y1,
                            double y2)
Finds the distance from the passed-in point to the specified trapezoid. Determines the distance as follows:

The trapezoid is defined by a top edge, two vertical sides, and a horizontal line at the origin of the y axis.

Parameters:
pix_x - the x value of the point
pix_y - the y value of the point
yorigin - the origin of the y axis
x1 - the x value of the first point that defines the trapezoid's top edge
x2 - the x value of the second point that defines the trapezoid's top edge
y1 - the y value of the first point that defines the trapezoid's top edge
y2 - the y value of the second point that defines the trapezoid's top edge
Returns:

pointsToPolygon

public static Polygon pointsToPolygon(Point[] points)
Takes an array of points and converts it into a Polygon object.

Parameters:
points - array of java.awt.Point objects
Returns:
polygon representing the points

brighter

public static Color brighter(Color color)
Calculates the color even when it is saturated (for example, when it is black or white).

Parameters:
color -
Returns:

darker

public static Color darker(Color color)
Calculates the color even when it is saturated (for example, when it is black or white).

Parameters:
color -
Returns:

colorsNear

public static boolean colorsNear(Color c1,
                                 Color c2)
Determines whether two colors are close to the same color. Used to determine whether two colors should be used as a foreground/background combination in default cases.

Parameters:
c1 - one color to check
c2 - the other color to check
Returns:
true if both colors are close; false otherwise.

iceil

public static int iceil(double val)
Takes the ceiling of a floating point value.

Parameters:
val -
Returns:

ifloor

public static int ifloor(double val)
Takes the floor of a floating point value.

Parameters:
val -
Returns:

calcError

public static double calcError(int base)
Calculates an appropriate error based on a precision value.

Parameters:
base -
Returns:

locateX

public static int locateX(double x,
                          double lx,
                          double ux)
Locates an x value within given x bounds.

Parameters:
x - the value to locate
lx - the lower bound to check against
ux - the upper bound to check against
Returns:
1 if below lower bound, 2 if above upper bound, 0 if within bounds

locateY

public static int locateY(double y,
                          double ly,
                          double uy)
Locates a y value within given y bounds.

Parameters:
y - the value to locate
ly - the lower bound to check against
uy - the upper bound to check against
Returns:
4 if below lower bound, 8 if above upper bound, 0 if within bounds

locateXY

public static int locateXY(double x,
                           double y,
                           double lx,
                           double ux,
                           double ly,
                           double uy)
Locates a point within given x and y bounds.

Parameters:
x - the x value of the point to locate
y - the y value of the point to locate
lx - the lower x bound to check against
ux - the upper x bound to check against
ly - the lower y bound to check against
uy - the upper y bound to check against
Returns:
0 if within all bounds; otherwise, returns a combination of the following values: 1 if below lower x bound, 2 if above upper x bound, 4 is below lower y bound, 8 if above upper x bound

isHTML

public static boolean isHTML(String s)
Deprecated. Use com.klg.jclass.util.swing.TextRenderer.isHTML() instead

Checks for HTML-encoded text. The check is the same as that in javax.swing.plaf.basic.BasicHTML.isHTMLString(). That call cannot be used because the class isn't public, so it is repeated here. For a String to be HTML-encoded, it needs to begin with "<html>".

Parameters:
s - the String to check for HTML encoding.
Returns:
true if String is encoded in html; false otherwise

countInString

public static int countInString(String token,
                                String target)
Counts the occurrences of String token within the larger target String.

Parameters:
target -
token -
Returns:

fudgeItalicLabel

public static void fudgeItalicLabel(Font font,
                                    Dimension dim)
Fudges the size of a label with italic fonts by making it a little bigger so that all of the italicized font can be seen. This is used to get around a jdk bug where italics aren't taken into account when calculating a label size.

Parameters:
dim -
font -

Copyright © 2004 Quest Software Inc..
All rights reserved.