Tuesday, August 12, 2014

Creating a 2D Fractal Landscape Generator in Python

     I've been fascinated with fractals for a good while now, so a few months ago, I decided to create a python program to generate fractal mountains. The program works by starting a triangle with random vertices. Then, it divides the triangle into 4 smaller triangles and randomizes the locations of the new vertices.

In the program, I had to create classes for both the nodes (vertices) and the connections between the nodes. I had to give the nodes attributes for both their Cartesian coordinates (location in the image) and their barycentric coordinates (location in the overall triangle). Then, I had to create a roughness parameter (to define how much the nodes would be randomized) and an iterations parameter to define how many times the program would repeat. Then, I made a "for" loop to run through each triangle of connections, create new nodes for the midpoints, connect them up with new edges, and randomize them. Here's some of the results:

Iterations = 6, roughness parameter = 0.2

Iterations = 5, roughness parameter = 0.2
Iterations = 6, roughness parameter = 0.25

Iterations = 5, roughness parameter = 0

You can have a look at the code here.

4 comments:

  1. Hi, the code is unavilable, could you please upload it again? i would ve very pleased.

    ReplyDelete
  2. +1 to that. Am interested in visualisation code.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete