Learn to Program with ZedLX

Free Online Programming Course for Beginners

How to Generate the Sierpinski Triangle [2/10]

2. The Three Inscribed Triangles

The next step in generating the Sierpinski Triangle is to draw three smaller triangles that are inscribed within the larger triangle. Each of these triangles is be colored to match the vertex it touches.
[Run the program by clicking the 'Run' button]

To draw the three smaller triangles, the mid-points of each side of the large triangle need to be computed first. In the source code, these mid-points are named mid12, mid13, mid23. Each mid-point is visualized in a different color: purple, azure, and olive.
[Run the program]

The three inscribed triangles are then drawn in colors that correspond to the vertices they touch: red, orange, and cyan.

Note that ZedLX does not have a built-in function for calculating mid-points. Instead, a user-defined function called MidPoint is provided at the end of the source code. This function calculates the mid-point of a line segment given two points. There are many possible implementations of this function, and the source code includes three variations, where only the last one is used in this program.

Loading...