Thursday, March 22, 2012

C# Sierpinski Triangle

Hello again lads!
Today we are going to program Sierpinski Triangle,

But What is Sierpinski Triangle? 
Sierpinski Triangle is a very nice triangle fractal, the fractal shows a triangle that has 3 triangles inside of it, and inside everyone of these triangles there is another 3 triangles and so on.

Here is a picture of it: 
















(This image is taken from Wikipedia)

Some of you may recognize this shape - it is a very common fractal.
Ok, so if you want to read more about this fractal here is a Wikipedia link

And Now to the Programming:
Ok, I  found an algorithm that represents this fractal, and it goes as follows:

1. Write three dots that represent the vertexes of the biggest triangle (the first triangle).
2. Start with a random point that is in the triangle borders.
3. Now choose a random point of the three vertexes you defined earlier, and calculate the distance between the current point and the vertex you choose, and print a point at the half of this distance.
Now use the point you printed and Do step 3 again and again, until you get the requested result.

I added another thing that makes it a bit more interesting. I painted the first vertexes with 3 different colors, red green and blue, and every time I'm printing a point I'm printing it with the color of the vertex that is chosen randomly.

This Program outputs this:


Now you need to remember that this algorithm is not 100% accurate!

Ok so here is the Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SerpinskiTri
{
    class Program
    {
        static void DotPrint(int x, int y, ConsoleColor col)
        {
            Console.ForegroundColor = col;
            Console.CursorLeft = x;
            Console.CursorTop = y;
            Console.Write(".");
        }
        static void Main(string[] args)
        {
            Console.CursorVisible = false;
            Console.SetWindowSize(80,40);
            int times = 0;
            Random rnd = new Random();
            Console.WriteLine();
            Console.Write("Enter Number of Times:");
            times = int.Parse(Console.ReadLine());
            int ChangeX = 35;
            int ChangeY = 20;
            int random = 0;
            for (int i = 0; i < times; i++)
            {
                random = rnd.Next(0,3);
                switch (random)
                {
                    case 0: //red dot
                        ChangeX = (ChangeX + 35) / 2; // the 35 is the x value of the red dot
                        ChangeY = (ChangeY + 1) / 2;
                        DotPrint(ChangeX, ChangeY, ConsoleColor.Red);
                        break;
                    case 1: // blue dot
                        ChangeX = (ChangeX + 1) / 2;
                        ChangeY = (ChangeY + 35) / 2;
                        DotPrint(ChangeX, ChangeY, ConsoleColor.Blue);
                        break;
                    case 2: // green dot
                        ChangeX = (ChangeX + 70) / 2;
                        ChangeY = (ChangeY + 35) / 2;
                        DotPrint(ChangeX, ChangeY, ConsoleColor.Green);
                        break;
                }
            }
            Console.ReadKey();
        }
    }
}


You can also download this project:
Download This Project (rar file)

Run Without Compiling: go to the project folder ->bin -> debug -> SerpinskiTri.exe

Questions = Comments!
I'll try to answer Asap.

1 comment: