
A Sierpinski triangle is a fractal in the shape of an equilateral triangle which is recursively subdivided into smaller and small triangles. In this article we’ll be discussing how to draw one in a C# console application.
The Draw Triangle Function
To create your triangle you’ll need two different functions, we’ll start with the Drawtriangle function. Start by writing a static void function called draw triangle. It should take two integers as arguments, these are the x and y coordinates of each triangle component.
static void DrawTriangle(int x, int y)
Inside the function set the cursors left location equal to x and its top location equal to y.
Console.CursorLeft = x; Console.CursorTop = y;
After that all you need to do is set what symbol you want the triangle to made of and set what you want the colour to be. I am using the colour red and an asterisk but feel free to experiment with whatever shapes and colours you want.
Console.ForegroundColor = ConsoleColor.Red; Console.Write("*");
This function will now set the coordinates of the cursor and draw the specified symbol in that new location.
The Sierpinski Function
Create a static void function called main. Inside the function first make the cursor invisible so it won’t get in the way of your triangle.
static void Sierpinski(string[] args) { Console.CursorVisible = false;
Next set the console window size to 80,40 to allow the triangle to be visible.
Console.SetWindowSize(80, 40);
Now you need to begin defining all the necessary variables when creating the triangle. Create an int variable called times and set it equal to 1000000, this is how many asterisk will be drawn to create the triangle. Next create a new random number variable called rnd and then an int variable called random set equal to 0.
int times = 1000000; Random rnd = new Random(); int random = 0;
After that create an int variable called X and set it equal to 35 and another int variable called Y set equal to 20.
int X = 35; int Y = 20;
Now you need to create a for loop that iterates through the times variable created earlier. Inside the loop have the random variable be randomly set from a number one to three and then have a switch statement beneath it. The switch statement should have three cases, all of them should call to Drawtriangle.
for (int i = 0; i < times; i++) { random = rnd.Next(0, 3); switch (random) { case 0: X = (X + 35) / 2; Y = (Y + 1) / 2; DrawTriangle(X, Y); break; case 1: X = (X + 1) / 2; Y = (Y + 35) / 2; DrawTriangle(X, Y); break; case 2: X = (X + 70) / 2; Y = (Y + 35) / 2; DrawTriangle(X, Y); break; } }
After you’ve completed the main function put in a call to read key to prevent the program from closing immediately and then you should have a working Sierpinski triangle.
Console.ReadKey();
Further Suggestions
To add a bit of extra challenge you could increase the number of triangles generated or make the amount generated based entirely on user input with the possibility of being able to generate a seemingly infinite number of them.
Good Luck!
Download the source code here.
If you have any comments or questions email them to me at nick@crumbsofcode.com