Chaos game – building triangles out of randomness

The Chaos game refers to the emergence of fractal patterns with random numbers when the selection of random numbers are subject to some constraints. Let's look at the rules of one of the simplest chaos games:

  1. We start by creating three points on a plane to form a triangle. 
  2. To begin the game, we draw a random point inside the triangle.
  3. We then roll a dice. Given the outcome, we move halfway between the last point and any one of the vertices of the triangle. For example, if the outcome is 1 or 2, we move halfway between the last point and vertex A. If the outcome is 3 or 4, we move halfway from the current point towards vertex B, or if the outcome is 5 or 6, we draw the next point halfway between the current point and vertex C, as shown in the following image. This is repeated over and over again:

Here is the surprise part of it. While all the points except for the three vertexes were selected at random, the end result is not a haphazard set of points but rather a fractal—a set of repeating patterns of triangles called the Sierpinski triangle, shown in the following screenshot. This, according to some mathematicians, is a glimpse into the orderliness of the universe hidden inside what appears to be otherwise chaotic:

Note that repeating this same rule inside a set of four points does not create a fractal. However, placing some specific kinds of restrictions on the choice of vertices produces a variety of interesting fractal shapes. You can read more about different varieties of fractals generated out of chaos games at https://en.wikipedia.org/wiki/Chaos_game.

Let us now code this program.  We first define the three vertices of the triangle, as shown in the preceding screenshot:

v1 = (float(WIDTH/2), 0.0)
v2 = (0.00, float(HEIGHT))
v3 = (float(WIDTH), float(HEIGHT))

Here, WIDTH and HEIGHT are the window dimensions.  

Our next task is to choose a random point inside our triangle as the starting point. This can be done using what are called barycentric coordinates.

Let V1, V2, V3 be the three vertices of a triangle. A point P inside the triangle can be expressed as P = aV1 + bV2 + cV3, where a+b+c=1 and a,b,c are each ≥ 0. If we know and b, we can calculate c as 1-a-b.

So we generate two random numbers, a and b, each in the range [0,1] so that their sum ≤ 1. If the sum of two random points exceeds 1, we replace a with 1-a and b with 1-b, so that their sum falls back below 1.  Then,  aV1 + bV2 + cV3 is uniformly distributed inside the triangle.

Now that we have the barycentric coordinates a, b, and c,  we can compute point P inside the triangle as  aV1 + bV2 + cV3. Here is the idea expressed in code (8.11_chaos_game.py):

def random_point_inside_triangle(v1, v2, v3):
a = random.random()
b = random.random()
if a + b > 1:
a = 1-a
b = 1-b
c = 1 - a -b
x = (a*v1[0])+(b*v2[0])+(c*v3[0]);
y = (a*v1[1])+(b*v2[1])+(c*v3[1]);
return (x,y)

We next define a method to calculate the halfway distance between two points:

def midway_point(p1, p2):
x = p1[0] + (p2[0] - p1[0]) //2
y = p1[1] + (p2[1] - p1[1]) //2
return (x,y)

This is a simple linear interpolation between two points based on the Pythagorean theorem. Note that in Python, the / operator does floating point division while // does integer division (dropping the remainder).

Next, we put the laws of the game in a method called get_next_point:

def get_next_point():
global last_point
roll = random.choice(range(6))+1
mid_point = None
if roll == 1 or roll == 2:
mid_point = midway_point(last_point, v1)
elif roll == 3 or roll == 4:
mid_point = midway_point(last_point, v2)
elif roll == 5 or roll == 6:
mid_point = midway_point(last_point, v3)
last_point = mid_point
return mid_point

Finally, we create a Tkinter canvas and define a method, update, to draw the individual pixels every 1 millisecond as follows:

def update():
x,y = get_next_point()
canvas.create_rectangle(x, y, x, y, outline="#FFFF33")
root.after(1, update)

Calling this update method creates the fractal pattern in our chaos game. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.166.255