Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

How to Generate the Sierpinski Triangle [2/10]

2. The Three Inscribed Triangles

The next step of generating the Sierpinski Triangle involves drawing three smaller triangles that are inscribed within the larger triangle. Each of the three inscribed triangles can 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.

Since ZedLX does not have a built-in function for calculating mid-points, a user-defined function called MidPoint is provided at the end of the source code. 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...