Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. package net.devtech.biome;
  2.  
  3. import javax.imageio.ImageIO;
  4. import java.awt.Color;
  5. import java.awt.Desktop;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9.  
  10. import static net.devtech.Rand.next;
  11. import static net.devtech.Rand.seed;
  12.  
  13. public class BiomeGen {
  14. private final int power;
  15. private final int size;
  16. private final int mod;
  17.  
  18. public BiomeGen(int power) {
  19. this.power = power;
  20. this.size = 1 << power;
  21. this.mod = size - 1;
  22. }
  23.  
  24. public int getBiome(int x, int y) {
  25. int rx = x >>> power;
  26. int ry = y >>> power;
  27.  
  28. int current = compute(rx, ry, x, y);
  29. int next;
  30.  
  31. int offsetx = 0;
  32. int offsety = 0;
  33.  
  34. if ((next = compute(rx - 1, ry, x, y)) < current) {
  35. offsetx = -1;
  36. offsety = 0;
  37. current = next;
  38. }
  39. if ((next = compute(rx, ry + 1, x, y)) < current) {
  40. offsetx = 0;
  41. offsety = 1;
  42. current = next;
  43. }
  44. if ((next = compute(rx + 1, ry, x, y)) < current) {
  45. offsetx = 1;
  46. offsety = 0;
  47. current = next;
  48. }
  49. if ((next = compute(rx, ry - 1, x, y)) < current) {
  50. offsetx = 0;
  51. offsety = -1;
  52. current = next;
  53. }
  54. if ((next = compute(rx - 1, ry - 1, x, y)) < current) {
  55. offsetx = -1;
  56. offsety = -1;
  57. current = next;
  58. }
  59. if ((next = compute(rx + 1, ry + 1, x, y)) < current) {
  60. offsetx = 1;
  61. offsety = 1;
  62. current = next;
  63. }
  64. if ((next = compute(rx + 1, ry - 1, x, y)) < current) {
  65. offsetx = 1;
  66. offsety = -1;
  67. current = next;
  68. }
  69. if (compute(rx - 1, ry + 1, x, y) < current) {
  70. offsetx = -1;
  71. offsety = 1;
  72. }
  73.  
  74. return next(seed(rx + offsetx, ry + offsety));
  75. }
  76.  
  77. private int compute(int rx, int ry, int x, int y) {
  78. long[] arr = {seed(rx, ry)};
  79. int ox = rx * size + (next(arr) & mod) - x;
  80. int oy = ry * size + (next(arr) & mod) - y;
  81. return ox * ox + oy * oy;
  82. }
  83.  
  84. public static void main(String[] args) throws IOException {
  85. BiomeGen gen = new BiomeGen(6);
  86. final int size = 2048;
  87. BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_4BYTE_ABGR);
  88. for (int i = 0; i < size; i++)
  89. for (int i1 = 0; i1 < size; i1++)
  90. image.setRGB(i, i1, new Color((gen.getBiome(i1, i)) & 255, (gen.getBiome(i1, i)) & 255, (gen.getBiome(i1, i)) & 255, (((gen.getBiome(i1, i)) & 255) == 0) ? 0 : 255).getRGB());
  91. File temp = File.createTempFile("bruh", ".png");
  92. ImageIO.write(image, "png", temp);
  93. Desktop.getDesktop().open(temp);
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement