Draw an Apollonian gasket with circles filled by random colors in C#

This example is similar to the example Draw an Apollonian gasket (or Apollonian packing) 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 and an array holding the colors that it should 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);
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.