/sierpinski_triangle

Small JavaScript program to generate a Sierpinski triangle (see https://sites.google.com/view/emanuelemusumeci/blog?authuser=0 for the result)

Primary LanguageJavaScript

Sierpinski triangle

The Sierpiński triangle (sometimes spelled Sierpinski), also called the Sierpiński gasket or Sierpiński sieve, is a fractal attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller equilateral triangles.

Animation showing the final result

To create this fractal, shape I used the "chaos game" method:

  1. Take three points in a plane to form a triangle.

  2. Randomly select any point inside the triangle and consider that your current position.

  3. Randomly select any one of the three vertex points.

  4. Move half the distance from your current position to the selected vertex.

  5. Plot the current position.

  6. Repeat from step 3.

The script is implemented in JavaScript. The canvas resets after 3000 iterations.

See my personal blog for the result.