STANDARD DISCLAIMER

Everything in this blog is my opinion and does not in any way, shape, or form represent the opinion or officially stated position of Microsoft, Google , or Kim Jong Il
this is fairly obvious when one considers I have no official capacity in any of these organizations.

Thursday, May 30, 2013

SIERPINSKI YEH TUNE KYA KI

 

image

OH YEAH!

You can generate this in code really easily

Start with a type

internal class VInfo
    {
        public int x;
        public int y;
        public Color c;
    }

int width = 2000;
int height = 1000;

Initialize thusly

Veritices[0].x = width / 2;
Veritices[0].y = 0;
  Veritices[0].c = Colors.Blue;
  

Veritices[1].x = 0;
Veritices[1].y = height;
  Veritices[1].c = Colors.Red;
  

Veritices[2].x = width;
Veritices[2].y = height;
  Veritices[2].c = Colors.Green;

So at this point you have the triangle set up right .

Now just run  a loop somewhere like so

Random r = new Random();
            Random InitPoint = new Random();
            VInfo pCurrent = new VInfo();
            pCurrent.x = InitPoint.Next(1000);
            pCurrent.y = InitPoint.Next(1000);

for (int i = 0; i < 10000000; i++ )
            {
                int p = r.Next() % 3;

                pCurrent.x = (pCurrent.x + Veritices[p].x) / 2;
                pCurrent.y = (pCurrent.y + Veritices[p].y) / 2;
                setpixel( bits,pCurrent.x,pCurrent.y,Veritices[p].c);

            }

So all you are doing  is getting a random point computing the halfway point between the random point and the vertices you need to and setting the pixel to that colour  AND…. repeat

Its really quite amazing how this simple random process can generate that image…

No comments: