Learn to Program with ZedLX

Free Online Programming Course for Beginners

How to Generate the Sierpinski Triangle [1/10]


How to Generate the Sierpinski Triangle

Sierpinski triangle in yellow

In this step-by-step guide, we will provide examples and detailed explanations on how to generate the Sierpinski Triangle using a computer program. These explanations can be applied to any other programming language as well.

What is the Sierpinski Triangle?

The Sierpinski Triangle is a fascinating fractal shape with a hollow structure. It exhibits fractal and recursive properties, which are best illustrated by its self-similarity: the smaller sections of the triangle resemble the entire triangle itself.

This pattern has been used as a decorative motif and was named after the Polish mathematician Waclaw Sierpiński, who studied its properties.

Drawing the Base Triangle

To draw the Sierpinski Triangle, we start by creating an ordinary triangle. This will be the foundation for our program.

The first step is to set the vertices of the triangle by specifying their coordinates. In the source code, these coordinates are defined in the first three statements. The next statement uses these vertices to draw a blue triangle on the screen.
[Run the program by clicking the 'Run' button]

The vertices, denoted as p1, p2, p3, are then drawn in different colors: the top vertex is 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 to see the triangle]

[Click the 'Next' button at the top of the page or press the F4 key to proceed.]

Loading... CodeMirror, ZedLX, Antlr // 

programming

programming

programming

programming

programming

programming