Wednesday, December 1, 2010

Prime Art Generators

Go here to see how to generate a set of Primes.


This is a simple method that traverses each row in the image and puts a black pixel at every point that is a Prime number.
private Bitmap CreateNormalPrimeArt(HashSet<int> primes, int width, int height)
{
    Bitmap bmp = new Bitmap(width, height);
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.FromArgb(25, 25, 76)))
        {
            gfx.FillRectangle(brush, new Rectangle(0, 0, width, height));
        }
    }
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (primes.Contains((y * height) + x))
                bmp.SetPixel(x, y, Color.White);
        }
    }
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.White))
        {
            gfx.DrawString("Prime Rows", new System.Drawing.Font("Calibri", 10), brush, new PointF(20, height - 20));
        }
    }
    return bmp;
}



The second is based on the Ulam Spiral.
private Bitmap CreateUlamSpiralPrimeArt(HashSet<int> primes, int width, int height)
{
    if (width != height)
    {
        if (width > height)
            width = height;
        else
            height = width;
    }
    if (width % 2 == 0)
    {
        width--;
        height--;
    }
    int index = 1;
    int sideLength = 2;
    int thisSide = 2;
    int turns = 0;
    Bitmap bmp = new Bitmap(width, height);
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.FromArgb(25, 25, 76)))
        {
            gfx.FillRectangle(brush, new Rectangle(0, 0, width, height));
        }
    }
    // set p to center
    Point p = new Point(width / 2, height / 2);
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (p.Y <= 5 || p.Y >= height - 5 || p.X <= 5 || p.X >= width - 5)
                break;
            if (primes.Contains(index))
                bmp.SetPixel(p.X, p.Y, Color.White);
            index++;
            thisSide--;
            int dir = turns % 4;
            if (thisSide == 0)
            {
                thisSide = sideLength;
                turns++;
                if (dir == 1 || dir == 3)
                    sideLength += 1;
            }
            if (dir == 0)
            {
                p.X = p.X + 1;
            }
            else if (dir == 1)
            {
                p.Y = p.Y - 1;
            }
            else if (dir == 2)
            {
                p.X = p.X - 1;
            }
            else if (dir == 3)
            {
                p.Y = p.Y + 1;
            }
        }
    }
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.White))
        {
            gfx.DrawString("Ulam Spiral", new System.Drawing.Font("Calibri", 10), brush, new PointF(20, height - 20));
        }
    }
    return bmp;
}



Third method creates nice spirals based on the Prime distribution. Enjoy.
private Bitmap CreateDSCPrimeArt(HashSet<int> primes, int width, int height)
{
    int index = 1;
    Bitmap bmp = new Bitmap(width, height);
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.FromArgb(25, 25, 76)))
        {
            gfx.FillRectangle(brush, new Rectangle(0, 0, width, height));
        }
    }
    // set p to center
    int max = primes.Max();
    Point p = new Point(width / 2, height / 2);
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (primes.Contains(index))
            {
                // calculate location of the next pixel from the center of the image
                // distance to the center of the image is based on the square root of the prime
                PointF pixel = new Point() { X = p.X, Y = p.Y };
                pixel.X += (float)(Math.Sin(index) * Math.Sqrt(index));
                pixel.Y += (float)(Math.Cos(index) * Math.Sqrt(index));

                if (!(pixel.Y <= 5 || pixel.Y >= height - 5 || pixel.X <= 5 || pixel.X >= width - 5))
                {
                    bmp.SetPixel((int)pixel.X, (int)pixel.Y, Color.White);
                }
            }
            index++;


            if (index > max)
                break;
        }
    }

    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.White))
        {
            gfx.DrawString("Prime Spiral", new System.Drawing.Font("Calibri", 10), brush, new PointF(20, height - 20));
        }
    }
    return bmp;
}

Update 2017-01-30
Fixed broken pictures, changed code to a cleaner format without line numbering.


Tuesday, November 30, 2010

Generate HashSet of Prime numbers, check if Prime. Basic

public static HashSet<int> GetPrimes(int max)
{
  HashSet<int> primes = new HashSet<int>();
  for (int i = 2; i < max; i++)
   if (IsPrime(i))
    primes.Add(i);
  return primes;
}

private static bool IsPrime(int candidate)
{
  bool prime = true;
  double sqr = Math.Sqrt(candidate);
  for (int i = 2; i <= sqr; i++)
  {
   if (candidate % i == 0)
   {
    prime = false;
    break;
   }
  }
  return prime;
}

Thursday, November 18, 2010

How to block advertisements in Windows Live Messenger

A friend asked me how I managed to get rid of the advertisements in Windows Live Messenger so I thought that I'd share it here.
I found this little trick some years ago when I got annoyed by the mouse over adverts that expanded outside the initial box that they appear in. I honestly do not remember where I found it and when I try to Google for the fix now it only finds some downloadable applications that should 'fix the ads'. They seem a little suspicious thou so I'll just share my fix here.

It seems that Windows Live Messenger gets all its advertisements from various feeds from msn.com, msads.net and live.com. Lucky us the feeds originate from subdomains to these addresses so we don't have to block the whole domains.

1. Open your "hosts" file that is located in one of the system folders.
  XP : C:\WINDOWS\system32\drivers\etc

Note that the file may be read-only so you should open it with administrative privileges. Right click and select "Run as Administrator".

2. Add the following lines to the end of the hosts file below the reference to localhost.
0.0.0.0 rad.msn.com
0.0.0.0 global.msads.net
0.0.0.0 rss.video.msn.com
0.0.0.0 ads1.msn.com
0.0.0.0 rad.live.com
0.0.0.0 specials.uk.msn.com

3. Save the file.

Done.

windows_live_messenger_after_no_advertisements_fixThe advertisements may still show for a little while after the fix has been applied. The next time Windows Live Messenger tries to download new advertisements it will only show a non clickable logo as seen in the picture to the right.
To speed up the process just restart Windows Live Messenger and the fix should be applied immediately.