fix corner bias in Vec3#random(random, true)
This commit is contained in:
parent
f7d9153ad8
commit
67bfafc5b8
@ -17,17 +17,29 @@ public record Vec3(double x, double y, double z) {
|
|||||||
assert Double.isFinite(x) && Double.isFinite(y) && Double.isFinite(z) : "x, y and z must be finite";
|
assert Double.isFinite(x) && Double.isFinite(y) && Double.isFinite(z) : "x, y and z must be finite";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@return a uniformly random vector with components in the range [-1, 1)}
|
||||||
|
*/
|
||||||
public static @NotNull Vec3 random(@NotNull RandomGenerator random) {
|
public static @NotNull Vec3 random(@NotNull RandomGenerator random) {
|
||||||
return random(random, false);
|
return new Vec3(
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Vec3 random(@NotNull RandomGenerator random, boolean unit) {
|
|
||||||
var vec = new Vec3(
|
|
||||||
Math.fma(2, random.nextDouble(), -1),
|
Math.fma(2, random.nextDouble(), -1),
|
||||||
Math.fma(2, random.nextDouble(), -1),
|
Math.fma(2, random.nextDouble(), -1),
|
||||||
Math.fma(2, random.nextDouble(), -1)
|
Math.fma(2, random.nextDouble(), -1)
|
||||||
);
|
);
|
||||||
return unit ? vec.unit() : vec;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@return a uniformly random unit vector}
|
||||||
|
*/
|
||||||
|
public static @NotNull Vec3 random(@NotNull RandomGenerator random, boolean unit) {
|
||||||
|
if (!unit) return random(random);
|
||||||
|
Vec3 vec;
|
||||||
|
double squared;
|
||||||
|
do {
|
||||||
|
vec = random(random);
|
||||||
|
squared = vec.squared();
|
||||||
|
} while (squared > 1);
|
||||||
|
return vec.div(Math.sqrt(squared));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull Vec3 reflect(@NotNull Vec3 vec, @NotNull Vec3 normal) {
|
public static @NotNull Vec3 reflect(@NotNull Vec3 vec, @NotNull Vec3 normal) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user