-
-
Save Spottedleaf/74f4e241012ca2fa67d8f1c7e8e34722 to your computer and use it in GitHub Desktop.
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
import java.util.Random; | |
public final class Test { | |
public static long getKey(final int x, final int y, final int z) { | |
return ((long)x & 0x7FFFFFF) | (((long)z & 0x7FFFFFF) << 27) | ((long)y << 54); | |
} | |
public static int getX(final long key) { | |
return (int) ((key << 37) >> 37); | |
} | |
public static int getY(final long key) { | |
return (int) (key >>> 54); | |
} | |
public static int getZ(final long key) { | |
return (int) ((key << 10) >> 37); | |
} | |
public static void test(final int x, final int y, final int z) { | |
final long key = getKey(x, y, z); | |
final int decx = getX(key); | |
final int decy = getY(key); | |
final int decz = getZ(key); | |
if (decx != x || decy != y || decz != z) { | |
throw new RuntimeException("Encoding error for coordinates (" + x + "," + y + "," + z + "): " + | |
"Encoded value: " + Long.toHexString(key) + ", decoded coordinates: (" + decx + "," + decy + "," + decz + ")"); | |
} | |
} | |
public static void main(final String[] args) { | |
final Random random = new Random(0x4C4541463E414C4CL); | |
for (long i = 0; i < 100_000_000; ++i) { | |
final int x = random.nextInt(67_108_864*2) - 67_108_864; | |
final int y = random.nextInt(1024); | |
final int z = random.nextInt(67_108_864*2) - 67_108_864; | |
test(x, y, z); | |
} | |
test(0, 0, 0); | |
test(30_000_000, 0, 30_000_000); | |
test(-30_000_000, 0, -30_000_000); | |
test(-30_000_000, 0, 0); | |
test(0, 0, -30_000_000); | |
test(-1, 255, -1); | |
test(-30_000_000, 255, -30_000_000); | |
test(30_000_000, 255, 30_000_000); | |
test(-67_108_864, 1023, -67_108_864); | |
test(67_108_863, 1023, 67_108_863); | |
System.out.println("Passed without incident"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment