[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Randomly color an Apollonian gasket in C#

[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.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.