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

In this tutorial, we will explain how to generate or draw the Sierpinski Triangle, with a computer program. This step-by-step guide includes multiple examples and explanations that are applicable across various programming languages.

What is the Sierpinski Triangle?

The Sierpinski Triangle is a fascinating fractal shape characterized by its hollow triangular structure. Its fractal and recursive properties are best illustrated through the concept of self-similarity: the smaller sections of the Sierpinski Triangle resemble the entire triangle itself.

To describe the Sierpinski Triangle recursively: it is composed of three smaller Sierpinski Triangles, and each of those is composed of three smaller Sierpinski Triangles, and so on. This recursive nature creates an infinitely intricate pattern.

While Sierpinski triangles were used as decorative patterns in the distant past, afterwards they were named after the Polish mathematician Waclaw Sierpiński.

Let's Draw the Sierpinski Triangle

To begin drawing the Sierpinski Triangle, we first need to determine the positions of its three vertices. These positions can be given by coordinates.

Setting the Vertices: In the source code, the coordinates for the three vertices of the triangle are given in the first three statements. The next statement draws an ordinary blue triangle connecting the given vertices.
[Run the program by clicking the 'Run' button]

Vertice Colors: Each vertex of the triangle — denoted as p1 , p2 , p3 — can be painted in a different color. The top vertex is colored red, the left vertex is orange, and the right vertex is cyan. This is accomplished by the last three statements in the source code.
[Run the program]

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

Loading...