[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: Color the solutions to the "Find the squares in C#" puzzle

[Color the solutions to the

The post Solve the "Find the squares in C#" puzzle explained how you can find solutions to the puzzle described in the post Puzzle: Find the squares in C#. This example shows how you can color the solutions so squares of the same size have the same color.

The bulk of the program is similar to the one described in Solve the "Find the squares in C#" puzzle. The following paragraphs describe the changes in this example.

First, the new program defines the following array of colors.

// Colors used to draw the squares. private Color[] Colors = { Color.Red, Color.Green, Color.Blue, };

The solution includes only four different sizes of squares, so this array only needs to include four colors. If you are working on a new problem and don't know how many colors you will need, simply add extra colors to the array so you are sure that you have enough.

To assign colors to squares of different sizes, the program needs to make a list of the possible sizes. It uses the following SideLengths list to keep track of the sizes if finds.

// The solutions' side lengths. private List SideLengths;

Now when the program finds a new square, it uses the following code to record that square's size.

// Save the side length. int side_length = (int)PointFDistance(Points[i], Points[j]); if (!SideLengths.Contains(side_length)) SideLengths.Add(side_length);

This code uses the PointFDistance method (which simply finds the distance between two points) to get the new square's side length and truncates the length into an integer. Rounding errors may make the lengths of similar squares not match exactly, but they should be within one unit of each other.

If the truncated length is not yet in the SideLengths list, the code adds it.

The final change to the program draws a square. The following code draws square number index.

// Get the side length. int side_length = (int)PointFDistance(pts[0], pts[1]); int index = SideLengths.IndexOf(side_length); // Make an appropriately colored brush and pen. Color fill_color = Color.FromArgb(64, Colors[index].R, Colors[index].G, Colors[index].B); using (Brush brush = new SolidBrush(fill_color)) { gr.FillPolygon(brush, pts.ToArray()); } using (Pen pen = new Pen(Colors[index], 0)) { gr.DrawPolygon(pen, pts.ToArray()); }

This code gets the square's side length and then finds the index of that length in the SideLengths list so it can use that index as an index into the Colors array.

The code then creates a color with the appropriate red, green, and blue components but with an alpha value 64 so the color is semi-transparent. It then fills the square with that color.

The snippet finishes by outlining the square with the original solid color.

If you run the program and click the Show Solutions button, you'll see that squares of different sizes have different colors. If you click the Show All button, you'll get a picture similar to the one at the top of this post showing all of the squares on top of each other.

Download the example to experiment with it and to see additional details.

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