Randomly color an Apollonian gasket in C#


[Apollonian gasket]

This example is similar to the example Draw an Apollonian gasket in C# except it fills the circles it draws with random colors. The program uses the following code to generate random colors.

// Return a random color.
private Random rand = new Random();
private Color[] Colors =
{
    Color.Red,
    Color.Green,
    Color.Blue,
    Color.Lime,
    Color.Orange,
    Color.Fuchsia,
    Color.Yellow,
    Color.LightGreen,
    Color.LightBlue,
    Color.Cyan,
};
private Color RandomColor()
{
    return Colors[rand.Next(0, Colors.Length)];
}

This code defines a Random object that it later uses to generate random numbers. It also defines an array holding the colors that it will use. The RandomColor method uses the Random object to generate a random index in the array and returns the corresponding color.

The program uses the RandomColor method to create brushes for filling the circles that it draws. For example, the following code shows how the program draws the large enclosing circle.

// Draw the big circle.
using (Brush the_brush = new SolidBrush(RandomColor()))
{
    big_circle.Draw(gr, the_brush);
}

That’s about all there is to it. Download the example and see the previous example for more details.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in algorithms, drawing, graphics, mathematics and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *