|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object wyvern.lib.Range
Encapsulates a Range [MIN..MAX] value, and can generate random numbers.
Field Summary | |
int |
max
|
int |
min
|
Constructor Summary | |
Range(int minimum,
int maximum)
Constructs a new Range. |
Method Summary | |
static int |
computeDistance(int x1,
int y1,
int x2,
int y2)
Does a fast (table-based) lookup of the distance between two nearby points, as an integer. |
static int |
computeDistance(java.util.List locs1,
java.util.List locs2)
Takes 2 location lists and computes the distance between the two closest points in the lists. |
static int |
computeDistance(Point p1,
Point p2)
Does a fast (table-based) lookup of the distance between two nearby points, as an integer. |
static boolean |
fiftyFifty()
Synonym for randomBoolean(). |
static void |
main(java.lang.String[] argv)
Tests the randomValue function. |
static Range |
parseRange(java.lang.String value)
Parses a range specifier into a min and max. |
static int |
percent()
Generates a random number from 1 to 100. |
static boolean |
randomBoolean()
Computes a 50/50 chance - a coin toss. |
int |
randomValue()
Returns a random value in this range. |
static int |
randomValue(int max)
Returns a random value between zero and max. |
static int |
randomValue(int min,
int max)
Returns a random int value in the specified range. |
static int |
randomValue(Range r)
Returns a random value in the specified range. |
static int |
rollDice(int num,
int diceSides)
Rolls a number of dice of the specified number of size and returns the result. |
static int |
rollDice(int num,
int diceSides,
int adjust)
Rolls a number of dice of the specified number of size and returns the result. |
static int |
rollDice(java.lang.String input)
|
java.lang.String |
toString()
Returns a String representation of this range (min..max). |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public int min
public int max
Constructor Detail |
public Range(int minimum, int maximum)
minimum
- the minimum value of the rangemaximum
- the maximum value of the rangeMethod Detail |
public int randomValue()
public static int randomValue(Range r)
r
- a Range with min and max values set
public static int randomValue(int min, int max)
min
- the min value (inclusive)max
- the max value (inclusive)public static int randomValue(int max)
max
- the max value (inclusive)public static boolean randomBoolean()
public static int percent()
public static boolean fiftyFifty()
public static Range parseRange(java.lang.String value)
value
- the string containing the range. Has to be
fairly strictly formatted: "min..max". Examples: "2..7",
"0..100".
java.lang.IllegalArgumentException
- if the value can't be parsedpublic static int rollDice(int num, int diceSides)
num
- the number of dice to rolldiceSides
- the number of sides on each diepublic static int rollDice(int num, int diceSides, int adjust)
num
- the number of dice to rolldiceSides
- the number of sides on each dieadjust
- the amount to add to the result after all the
dice are rolled. This shifts the center of the bell curve;
for example, to generate a bell curve around zero, you could
pass 6 ten-sided dice (range: 6-60, average 33) with an adjust
of -33.public static int rollDice(java.lang.String input)
public static int computeDistance(int x1, int y1, int x2, int y2)
See performance notes in the overloaded version of computeDistance().
x1
- first point xy1
- first point yx2
- second point xy2
- second point y
public static int computeDistance(Point p1, Point p2)
NOTE: Subsequent profiling has shown that if you need to compute the distance between two points, it's much faster to inline the computation with the following code:
int xdiff = x2 - x1; if ( xdiff < 0 ) xdiff = -xdiff; int ydiff = y2 - y1; if ( ydiff < 0 ) ydiff = -ydiff; double dist = Math.sqrt ( (xdiff << 2) + (ydiff << 2) );If you need the integer distance, the last line can be:
int dist = (int) Math.sqrt ( (xdiff << 2) + (ydiff << 2) );The inlined calculation is over twice as fast, even though it's using floating point, probably because of the overhead of the method calls that Range.computeDistance() uses.
Bottom line is: avoid using this method unless you're lazy. (Realistically, they're both pretty fast, so it doesn't matter that much.)
p1
- first pointp2
- second point
public static int computeDistance(java.util.List locs1, java.util.List locs2)
locs1
- first location list (containing Point objects)locs2
- second location list (containing Point objects)
public java.lang.String toString()
public static void main(java.lang.String[] argv)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |