Mandelbrot Minecraft

Mandelbrot Set in Minecraft

mandebrot.png

The Mandelbrot set is a famous set of numbers that looks really cool. Represented as a graph, it has infinite complexity and its features repeat. It is a fractal. This algorithm is an "escape time" algorithm, because it tests how many iterations it takes for that coordinate to "escape" to infinity if it does at all.

The following code from the Wikipedia entry on the Mandelbrot set helped a lot:

For each pixel (Px, Py) on the screen, do:
{
  x0 = scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.5, 1))
  y0 = scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1, 1))
  x = 0.0
  y = 0.0
  iteration = 0
  max_iteration = 1000
  while (x*x + y*y < 2*2  AND  iteration < max_iteration) {
    xtemp = x*x - y*y + x0
    y = 2*x*y + y0
    x = xtemp
    iteration = iteration + 1
  }
  color = palette[iteration]
  plot(Px, Py, color)
}

To scale Px and Py to the scale of the Mandelbrot set:

(Px * 3.5)/width - 2.5 = x0
(Py * 2)/width - 1 = y0

Here is the MakeCode JavaScript to draw this fractal in Minecraft:

let blocktype = 0
let xtemp = 0
let y0 = 0
let x0 = 0
let its = 0
let y = 0
let x = 0
let maxits = 0
let Px = 0
let Py = 0
let height = 0
let width = 0
let blocklist: number[] = []
player.onChat("mandelbrot", function () {
blocklist = [blocks.block(Block.LimeConcrete), blocks.block(Block.BlueStainedGlass), blocks.block(Block.PurpleConcrete), blocks.block(Block.BlueConcrete), blocks.block(Block.YellowConcrete), blocks.block(Block.Obsidian), blocks.block(Block.LightBlueConcrete), blocks.block(Block.WhiteStainedGlass), blocks.block(Block.YellowStainedGlass)]
  width = 200
  height = 100
  Py = 0
  Px = 0
  maxits = 50
  while (Py <= height) {
    x = 0
    y = 0
    its = 0
    x0 = Px * 3.5 / width - 2.5
    y0 = Py * 2 / height - 1
    while (Math.pow(x, 2) + Math.pow(y, 2) < 4 && its < maxits) {
      xtemp = Math.pow(x, 2) - Math.pow(y, 2) + x0
      y = 2 * (x * y) + y0
      x = xtemp
      its += 1
      }
    blocktype = blocklist[its % blocklist.length]
    blocks.place(blocktype, positions.create(Px, Py, 10))
    Px += 1
    if (Px > width) {
      Py += 1
      Px = 0
      }
    }
})