Title: Randomly color an Apollonian gasket in C#
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 to experiment with it and to see additional details.
|