public final class Frac
extends java.lang.Object
The Frac class contains constants and methods related to fracunits,
Cell2D's units of "continuous" length and time. Typically, one fracunit of
length corresponds to one pixel on the screen, and one fracunit of time
corresponds to one frame. Fracunits are units of the primitive type
long
, and one fracunit is equal to 2 to the power of 32, or
4294967296. In other words, fracunit arithmetic is fixed-point number
arithmetic in which numbers have 32 bits after the decimal point.
Fracunit-scale numbers may be correctly added and subtracted with the + and -
operators, and they may be correctly multiplied and divided by
non-fracunit-scale numbers with the * and / operators. However, other
operations, such as multiplying two fracunit-scale numbers, require the use
of dedicated methods in the Frac class.
Modifier and Type | Field and Description |
---|---|
static int |
BITS
32, the number of bits by which an integer must be shifted left to be
converted to fracunit scale, and by which a number in fracunit scale must
be shifted right to be rounded down and converted to an integer.
|
static long |
UNIT
One fracunit, equal to 2 to the power of BITS, or 4294967296.
|
Modifier and Type | Method and Description |
---|---|
static long |
ceil(long a)
Returns the smallest fracunit-scale integer that is greater than or equal
to the specified fracunit-scale number.
|
static long |
div(long a,
long b)
Returns the first specified fracunit-scale number divided by the second.
|
static long |
floor(long a)
Returns the largest fracunit-scale integer that is less than or equal to
the specified fracunit-scale number.
|
static int |
intCeil(long a)
Returns the smallest integer that is greater than or equal to the
specified fracunit-scale number.
|
static int |
intFloor(long a)
Returns the largest integer that is less than or equal to the specified
fracunit-scale number.
|
static int |
intRound(long a)
Returns the equivalent of the specified fracunit-scale number, rounded to
the nearest fracunit, as an
int . |
static long |
mul(long a,
long b)
Returns the product of the two specified fracunit-scale numbers.
|
static long |
round(long a)
Returns the specified number rounded to the nearest fracunit.
|
static long |
sqrt(long a)
Returns the square root of the specified fracunit-scale number.
|
static double |
toDouble(long a)
Returns the equivalent of the specified fracunit-scale number as a
double . |
static long |
units(double a)
Returns the equivalent of the specified
double in fracunit
scale. |
public static final int BITS
public static final long UNIT
public static long units(double a)
double
in fracunit
scale.a
- The double
to be converted to fracunit scaledouble
in fracunit
scalepublic static double toDouble(long a)
double
.a
- The fracunit-scale number to be converted to a double
double
public static long mul(long a, long b)
a
- The first numberb
- The second numberpublic static long div(long a, long b)
a
- The first numberb
- The second numberpublic static long sqrt(long a)
a
- The numberpublic static long round(long a)
a
- The numberpublic static int intRound(long a)
int
.a
- The fracunit-scale number to be rounded and converted to an
int
int
public static long ceil(long a)
a
- The numberpublic static int intCeil(long a)
a
- The numberpublic static long floor(long a)
a
- The numberpublic static int intFloor(long a)
a
- The number