📄 boidsutils.java
字号:
package example.boids;
import java.util.Random;
class BoidsUtils {
private static Random random = new Random();
private BoidsUtils() {
}
static int random(int scale) {
int randomInt = random.nextInt();
// extend to long, but remove sign-extend
long randomIntAsLong = (long) randomInt & 0xFFFFFFFFL;
int result = (int) ( (randomIntAsLong * (long) scale) >> 32);
return result;
}
// This seems to produce the square root rounded down to the next
// integer, except for numbers very near the next square, when we
// get the square root rounded up to the next integer. That's good
// enough for our purposes here anyway. In use in the boids MIDlet,
// it averages about 14 iterations per use.
static int squareRoot(int square) {
int guess = square;
if (square < 0) {
throw new IllegalArgumentException(
"Negative argument to squareRoot");
}
else if (square != 0) {
int oldGuess;
do {
// Newton's method
oldGuess = guess;
guess = (oldGuess + square / oldGuess) >> 1;
}
while ( (guess < oldGuess) && (guess > 0));
}
return guess;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -