40. Stars

This is a relatively simple exercise in keeping track of points. To find the polygon's vertices, you can make a theta variable loop over angles using sines and cosines to find the vertices.

Because of the way C# calculates angles, and because of the fact that Y coordinates increase downward, theta should initially have the value –π/2 if you want the peak of an odd-sided polygon to be on the top.

When you enter the number of sides and the skip number and click Go, the example solution executes the following code:

// Get the parameter to draw a new start and refresh.
private void PrepareStar()
{
NumSides = int.Parse(numSidesTextBox.Text);
Skip = int.Parse(skipTextBox.Text);
starPictureBox.Refresh();
}

This event handler simply parses the values that you entered and then refreshes the program's PictureBox to make it draw using the new parameters. The following code shows how the program draws its polygon and star:

// Draw the star.
private void starPictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

// Get positioning values.
PointF center = new PointF(
starPictureBox.ClientSize.Width / 2f,
starPictureBox.ClientSize.Height / 2f);
const float margin = 5;
float radius = Math.Min(
starPictureBox.ClientSize.Width / 2f,
starPictureBox.ClientSize.Height / 2f) - margin;

// Draw the poylgon.
double theta = -Math.PI / 2.0;
double dtheta = 2 * Math.PI / NumSides;
PointF[] points = new PointF[NumSides];
for (int i = 0; i < NumSides; i++)
{
points[i] = new PointF(
(float)(center.X + radius * Math.Cos(theta)),
(float)(center.Y + radius * Math.Sin(theta)));
theta += dtheta;
}
e.Graphics.DrawPolygon(Pens.Red, points);

// Draw the star.
for (int i = 0; i < NumSides; i++)
e.Graphics.DrawLine(Pens.Blue,
points[i], points[(i + Skip) % NumSides]);
}

This code finds the center of the program's PictureBox. It then calculates a radius for the polygon. It sets the radius, which is the distance between the polygon's center and its vertices, to be half of the smaller of the PictureBox control's width and height, minus a margin.

Next, the code initializes the theta variable to –π/2. It sets dtheta to 2π divided by the polygon's number of sides. The loop can use that value to make theta cover 2π values as the program draws the polygon.

The code then loops over the polygon's sides. It uses theta to find the next vertex and then increases theta by dtheta.

After it generates the polygon's vertices, the program simply draws them to display the polygon.

To draw the star, the program loops through the polygon's points, and draws a line between each point and the one that comes Skip positions later. The code uses the modulus operator % to ensure that the points' indices remain within the points array.

Download the Stars example solution to see additional details. If you experiment with the program, you'll find that it produces a disconnected star if the greatest common divisor of the number of polygon sides and the skip number lies between those values. In other words, if the polygon has P sides and the skip number is S, then the star is disconnected if 1 < GCD(P, S) < P.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset