Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement linear patterns using RandomPattern + SimpleRandom (#2630)
- Loading branch information
Showing
8 changed files
with
141 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
worldedit-core/src/main/java/com/fastasyncworldedit/core/math/random/Linear2DRandom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.fastasyncworldedit.core.math.random; | ||
|
||
import static com.fastasyncworldedit.core.math.random.Linear3DRandom.doubleDiv; | ||
import static java.lang.Math.floorDiv; | ||
|
||
/** | ||
* A {@link SimpleRandom} that deterministically maps coordinates | ||
* to values. | ||
* @since TODO | ||
*/ | ||
public class Linear2DRandom implements SimpleRandom { | ||
private final int xScale; | ||
private final int zScale; | ||
|
||
/** | ||
* Creates a new {@link Linear2DRandom} instance | ||
* | ||
* @param xScale the scale applied to the x component of a coordinate | ||
* @param zScale the scale applied to the z component of a coordinate | ||
*/ | ||
public Linear2DRandom(final int xScale, final int zScale) { | ||
this.xScale = xScale; | ||
this.zScale = zScale; | ||
} | ||
|
||
@Override | ||
public double nextDouble(final int x, final int y, final int z) { | ||
return nextDouble(x, y, z, 1d); | ||
} | ||
|
||
@Override | ||
public double nextDouble(final int x, final int y, final int z, double bound) { | ||
double index = (doubleDiv(x, this.xScale) + doubleDiv(z, this.zScale)) % bound; | ||
if (index < 0) { | ||
index += bound; | ||
} | ||
return index; | ||
|
||
} | ||
|
||
@Override | ||
public int nextInt(final int x, final int y, final int z, final int bound) { | ||
int index = (floorDiv(x, this.xScale) + floorDiv(z, this.zScale)) % bound; | ||
if (index < 0) { | ||
index += bound; | ||
} | ||
return index; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
worldedit-core/src/main/java/com/fastasyncworldedit/core/math/random/Linear3DRandom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.fastasyncworldedit.core.math.random; | ||
|
||
import static java.lang.Math.floorDiv; | ||
|
||
/** | ||
* A {@link SimpleRandom} that deterministically maps coordinates | ||
* to values. | ||
* @since TODO | ||
*/ | ||
public class Linear3DRandom implements SimpleRandom { | ||
|
||
private final int xScale; | ||
private final int yScale; | ||
private final int zScale; | ||
|
||
/** | ||
* Creates a new {@link Linear3DRandom} instance | ||
* | ||
* @param xScale the scale applied to the x component of a coordinate | ||
* @param yScale the scale applied to the y component of a coordinate | ||
* @param zScale the scale applied to the z component of a coordinate | ||
*/ | ||
public Linear3DRandom(final int xScale, final int yScale, final int zScale) { | ||
this.xScale = xScale; | ||
this.yScale = yScale; | ||
this.zScale = zScale; | ||
} | ||
|
||
@Override | ||
public double nextDouble(final int x, final int y, final int z) { | ||
return nextDouble(x, y, z, 1d); | ||
} | ||
|
||
@Override | ||
public double nextDouble(final int x, final int y, final int z, double bound) { | ||
double index = (doubleDiv(x, this.xScale) + doubleDiv(y, this.yScale) + doubleDiv(z, this.zScale)) % bound; | ||
if (index < 0) { | ||
index += bound; | ||
} | ||
return index; | ||
} | ||
|
||
// used to avoid explicit conversion at call site | ||
static double doubleDiv(double dividend, double divisor) { | ||
// add a minimal value to avoid too many integral values hitting the exact weight of an entry in SimpleRandomCollection | ||
return Math.nextUp(dividend) / divisor; | ||
} | ||
|
||
@Override | ||
public int nextInt(final int x, final int y, final int z, final int bound) { | ||
int index = (floorDiv(x, this.xScale) + floorDiv(y, this.yScale) + floorDiv(z, this.zScale)) % bound; | ||
if (index < 0) { | ||
index += bound; | ||
} | ||
return index; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters