Sierpinski Triangle using Graphics

Sierpinski triangle is a fractal and attractive fixed set with the overall shape of an equilateral triangle. It subdivides recursively into smaller triangles.

Approach:

  • In the given segment of codes, a triangle is made and then draws out three other adjacent small triangles till the terminating condition which checks out whether the height of the triangle is less than 5 pixels returns true.
  • We only need to verify whether a given triangle is smaller than 5 pixels since beyond that the triangles would start converging at fixed points.
  • A counter colorVal is defined for in response to the aesthetic need of the triangle and in all, it cycles through all the available colours by iterating every triangle set.
  • Using this methodology we can also further implement a fractal zoom and hypothetically provide an infinite zoom later.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to implement
// Sierpinski Triangle using Graphics
  
#include <math.h>
#include <stdlib.h>
#include <winbgim.h>
  
#define Y 900
#define X 1600
  
// Defining a function to draw a triangle
// with thickness 'delta'
void triangle(float x, float y,
              float h, int colorVal)
{
    setcolor(colorVal % 15 + 1);
  
    for (float delta = 0; delta > -5; delta -= 1) {
        line(x - (h + delta) / sqrt(3),
             y - (h + delta) / 3,
             x + (h + delta) / sqrt(3),
             y - (h + delta) / 3);
        line(x - (h + delta) / sqrt(3),
             y - (h + delta) / 3,
             x,
             y + 2 * (h + delta) / 3);
        line(x,
             y + 2 * (h + delta) / 3,
             x + (h + delta) / sqrt(3),
             y - (h + delta) / 3);
    }
}
  
// Defining a function to draw
// an inverted triangle
// with thickness 'delta'
void trianglev2(float x, float y,
                float h, int colorVal)
{
    setcolor(colorVal % 15 + 1);
  
    for (float delta = 0; delta > -1 + 5; delta -= 1) {
  
        line(x - (h + delta) / sqrt(3),
             y + (h + delta) / 3,
             x + (h + delta) / sqrt(3),
             y + (h + delta) / 3);
        line(x - (h + delta) / sqrt(3),
             y + (h + delta) / 3,
             x,
             y - 2 * (h + delta) / 3);
        line(x,
             y - 2 * (h + delta) / 3,
             x + (h + delta) / sqrt(3),
             y + (h + delta) / 3);
    }
}
  
// A recursive function to draw out
// three adjacent smaller triangles
// while the height is greater than 5 pixels.
int drawTriangles(float x = X / 2,
                  float y = 2 * Y / 3,
                  float h = Y / 2,
                  int colorVal = 0)
{
  
    if (h < 5) {
        return 0;
    }
  
    if (x > 0 && y > 0 && x < X && y < Y) {
        triangle(x, y, h, colorVal);
    }
  
    drawTriangles(x,
                  y - 2 * h / 3,
                  h / 2,
                  colorVal + 1);
    drawTriangles(x - h / sqrt(3),
                  y + h / 3,
                  h / 2,
                  colorVal + 1);
    drawTriangles(x + h / sqrt(3),
                  y + h / 3,
                  h / 2,
                  colorVal + 1);
  
    return 0;
}
  
// Driver code
int main()
{
    initwindow(X, Y);
    trianglev2(X / 2, 2 * Y / 3, Y, 2);
  
    drawTriangles();
    getch();
    closegraph();
  
    return 0;
}

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.