# Fractal Terrains

 Hexagonal fractal terrains: 32x32, 64x64 and 128x128 mesh grids (Java applet, 2kb). Click terrain to get a new random terrain. You'll get a new terrain every times you reload these WRLs too. Terrains with JavaScript (2kb): 32x32 and 64x64 mesh grids. "How do Terrains grow" (click terrain to get new step of iterations). If you didn't install a VRML plugin yet see 3D Mountains with Java1.1 applets.

# Midpoint displacement algorithm

To get a 2D "mountain" you take an elastic string, then a random vertical displacement is applied to its middle point. The process is repeated recursively to the middle point of every new segment. The random displacement decreases two times each iteration.

Look for more explanations about Fractal Terrains:
Fractal Landscapes by Paul Bourke
What are Fractal Terrains? (Fractal art FAQ)

 To get a 3D terrain, we take an "elastic membrane" with (NxN) triangular mesh grid, where N = 2k (i.e. N = 32, 64...). Then for every big triangle ABC a random vertical (Y axis) displacement is applied to the middle point of each side (an apex of new small triangles) e.g.:     Ya = (YA + YB )/2 + Ci(Rand - 0.5),     Ci+1 = Ci / H H = 2,   Co determines Y scale of our terrains.
 The process is repeated recursively. The random displacement decreases by H each iteration. Boundary points of the grid are fixed at Y = 0 (You can fix any point at any Y value also). I bend rhomb into a square (because of an ElevationGrid node) and set Y = 0.1 Rand for Y < 0. Finaly Terrains vertexes are painted in white, brown, green or blue colors in accordance with Y values (You can play with colors). This procedure is realized as simple cycles (see free sources for details):
```n=32; H=2;  n1=n+1;  nc=n; Max=n1*n1;
M = new MFFloat();   M.length = Max;
while ( (nc /= 2) >= 1) {
ncn1 = nc*n1;
for (j =ncn1; j < Max; j += ncn1+ncn1){
for (i = nc; i< n; i += nc+nc) {
M[i+j] = (M[i+j+nc-ncn1] + M[i+j-nc+ncn1])/2.+Rand();
M[i+j+nc] = (M[i+j+nc+ncn1] + M[i+j+nc-ncn1])/2.+Rand();
M[i+j+ncn1] = (M[i+j-nc+ncn1] + M[i+j+nc+ncn1])/2.+Rand();
}}
}
```
This algorithm uses random numbers, therefore every time you will get a new terrain. You can reproduce a terrain if you have a reproducible sequence of random numbers. For different H we will get terrains with different fractal dimensions. Terrains are fractals because by construction any region is self similar under shrinking (or stretching) by 2 times.

For a grid with square symmetry one can use a method similar to "guessing" algorithm for the Mandelbrot set.

3D Lab     updated 9 May 2001