Sierpinski Triangle using Graphics

23 Oct, 2019
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:
// 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 codeint main(){    initwindow(X, Y);    trianglev2(X / 2, 2 * Y / 3, Y, 2);      drawTriangles();    getch();    closegraph();      return 0;}
Output:
computer-graphics
pattern-printing
Advanced Computer Subject
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
System Design Tutorial
ML | Linear Regression
Docker - COPY Instruction
Reinforcement learning
Supervised and Unsupervised learning
Decision Tree Introduction with example
Getting started with Machine Learning
How to Run a Python Script using Docker?
ML | Monte Carlo Tree Search (MCTS)
Copying Files to and from Docker Containers