Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

How to Generate the Sierpinski Triangle [1/10]


How to Generate the Sierpinski Triangle

Sierpinski triangle in yellow

This tutorial explains how to generate or draw the Sierpinski Triangle, with a computer program. This is a detailed step-by-step guide, with multiple examples. The provided explanations are valid for generating the Sierpinski Triangle in any programming language.

The Sierpinski triangle is an intriguing, hollow triangle shape, that has a fractal nature. The fractal and recursive properties of the Sierpinski triangle are best understood through the concept of self-similarity: the smaller parts of the Sierpinski triangle look exactly like the entire triangle. As such, the Sierpinski triangle can be best described in a recursive manner: the shape of the Sierpinski triangle is made of three smaller Sierpinski triangles, and each of those three is made of three smaller Sierpinski triangles, and each of those three is made of three smaller Sierpinski triangles, etc...

While Sierpinski triangles were used as decorative patterns a long time ago, afterwards they were named after the Polish mathematician Waclaw Sierpiński.

Let's Draw the Sierpinski Triangle

To start drawing the Sierpinski triangle, the positions of three points that are the triangle's vertices need to be known. The positions of the three points can be given by coordinates.

In the source code, the coordinates of three vertices of a triangle are given by the first three statements. The next statement draws an ordinary blue triangle between the given vertices.
[Run the program by clicking the 'Run' button]

Each of the triangle's vertices p1 , p2 , p3 can be painted in a different color. The top one is painted red, the left one is orange and the right one is painted cyan. This is accomplished by the last three statements of the source code.
[Run the program]

[Click on the 'Next' button (on the page top) to read the next page, or press the F4 key]

Loading...