Beautifully Simple Code Generates Realistic Terrain With Fractals

Building a world is no easy task—the one we live in took somewhere between six days and 13 billion years, depending on who you ask. If you ask self-­described JavaScript junkie Hunter Loftis, though, he might have a different opinion. Loftis developed a startlingly simple method for generating beautiful, realistic terrain, and he’s sharing it with the whole Internet.

In a process that uses only 120 lines of code, Loftis’ directions turn a flat grid into one of nearly infinite complexity. The code relies on fractal imaging, a geometric concept in which each section of the shape has the same statistical character as the whole shape. This is one of the best ways to mathematically model seemingly chaotic patterns, such as crystal growth or coastal erosion. In this case, Loftis is using it to model entire chunks of terrain. Here is a demo of the randomized terrain generation. Hit the refresh button to generated new combinations of enchanting hills, mountains, canyons, and valleys.

Videos by VICE

Modeling realistic terrain in and of itself isn’t the most momentous thing about this project, though. The real kicker is the simplicity of the code. It works by taking a simple grid and continually dividing it into smaller and smaller grids. As each smaller grid is created, the randomizing function splits the grids into alternating squares and diamonds. Then it creates the new randomized terrain for the next set of grids based on data points from the previous square or diamond. You can read Loftis’ in­-depth explanation here

The result is truly random terrain, but with graduated areas of randomization. This algorithm keeps randomly generated terrain from erecting giant towers one pixel wide next to a plane pockmarked with deep, pixelated pits. In short, it makes the terrain look natural.

Loftis closes his explanation with a challenge: “If you’re anything like me, the results of this simple algorithm leave you itching to go build an online Terragen, a jetpack­-based first person shooter, fishing simulator, MMORPG, etc. This single­cube, canvas­projected demo practically begs for extension.”

And the script­-slinging population of the web is responding:

Simple, elegant solutions like these prompt an interesting line of questioning: is the world we live in actually fractal in nature? Could the base code of the universe actually be only a few simple lines? Nobody knows, but it’ll probably be someone like Hunter Loftis who figures it out. 

Related:

Artist Brings Historic Battle to Life With 3D Rendering

Drive Into the Ocean with These Maps App Glitches

These iOS Map Glitches Show The Warped Beauty Of Machine Error