Design

# Beautifully Simple Code Generates Realistic Terrain With Fractals

Loftis developed a startlingly simple method for generating beautiful terrain, and he’s sharing it with the whole Internet.

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.

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:

@HunterLoftis First pass at an interactive version of your really neat terrain demo - http://t.co/UaWGBS9uhz - thanks for sharing.

— Callum Prentice (@callumprentice) May 13, 2014

Processing port of realistic terrain: http://t.co/GKHNZKnhGf by Jerome Herr

— Hunter Loftis (@HunterLoftis) May 14, 2014

@hunterloftis Ported your nifty terrain generator to ObjC. https://t.co/SglKGqmmXB

— Chris Cieslak (@cieslak) May 14, 2014

Made a "3D terrain printer" based on @HunterLoftis work: http://t.co/Sc2dhTp4Tv

— Andrei Kashcha (@anvaka) May 20, 2014

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