I wanted to draw a 5 pointed star in C# and here is how I finally did it.

I leave it as an exercise (some hints are given in the code) to work out how I did this.

It was fun to do (but a bit hard, my trig is getting worned out :) ) but if you want to ask questions please do.

For the installation of the code please refer to this snippet: http://www.daniweb.com/software-development/csharp/code/359360

# Draw any star you want.

```
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Draw
{
public class DrawingForm : Form
{
public DrawingForm() // contructor
{
//InitializeComponent
this.Text = "My drawings"; // title of window form
this.Size = new Size(600, 600); // size of window form
this.Paint += new PaintEventHandler(MyPainting); // install handler
}
private void MyPainting(object sender, PaintEventArgs e)
{
Graphics G = e.Graphics;
G.SmoothingMode = SmoothingMode.HighQuality;
// init 4 stars
PointF[] Star1 = Calculate5StarPoints(new PointF(100f, 100f), 50f, 20f);
SolidBrush FillBrush = new SolidBrush(Color.Blue);
G.FillPolygon(FillBrush, Star1);
G.DrawPolygon(new Pen(Color.Yellow, 5), Star1);
PointF[] Star2 = Calculate5StarPoints(new PointF(200f, 150f), 100f, 20f);
HatchBrush pat = new HatchBrush(HatchStyle.DiagonalBrick, Color.RosyBrown, Color.RoyalBlue);
G.FillPolygon(pat, Star2);
PointF[] Star3 = Calculate5StarPoints(new PointF(350f, 300f), 200f, 100f);
LinearGradientBrush lin = new LinearGradientBrush(new Point(350, 100), new Point(350, 500),
Color.Salmon, Color.SeaGreen);
G.FillPolygon(lin, Star3);
PointF[] Star4 = Calculate5StarPoints(new PointF(140f, 400f), 120f, 10f);
G.DrawPolygon(new Pen(Color.Red, 3), Star4);
}
/// <summary>
/// Return an array of 10 points to be used in a Draw- or FillPolygon method
/// </summary>
/// <param name="Orig"> The origin is the middle of the star.</param>
/// <param name="outerradius">Radius of the surrounding circle.</param>
/// <param name="innerradius">Radius of the circle for the "inner" points</param>
/// <returns>Array of 10 PointF structures</returns>
private PointF[] Calculate5StarPoints(PointF Orig, float outerradius, float innerradius)
{
// Define some variables to avoid as much calculations as possible
// conversions to radians
double Ang36 = Math.PI / 5.0; // 36Â° x PI/180
double Ang72 = 2.0 * Ang36; // 72Â° x PI/180
// some sine and cosine values we need
float Sin36 = (float)Math.Sin(Ang36);
float Sin72 = (float)Math.Sin(Ang72);
float Cos36 = (float)Math.Cos(Ang36);
float Cos72 = (float)Math.Cos(Ang72);
// Fill array with 10 origin points
PointF[] pnts = { Orig, Orig, Orig, Orig, Orig, Orig, Orig, Orig, Orig, Orig };
pnts[0].Y -= outerradius; // top off the star, or on a clock this is 12:00 or 0:00 hours
pnts[1].X += innerradius * Sin36; pnts[1].Y -= innerradius * Cos36; // 0:06 hours
pnts[2].X += outerradius * Sin72; pnts[2].Y -= outerradius * Cos72; // 0:12 hours
pnts[3].X += innerradius * Sin72; pnts[3].Y += innerradius * Cos72; // 0:18
pnts[4].X += outerradius * Sin36; pnts[4].Y += outerradius * Cos36; // 0:24
// Phew! Glad I got that trig working.
pnts[5].Y += innerradius;
// I use the symmetry of the star figure here
pnts[6].X += pnts[6].X - pnts[4].X; pnts[6].Y = pnts[4].Y; // mirror point
pnts[7].X += pnts[7].X - pnts[3].X; pnts[7].Y = pnts[3].Y; // mirror point
pnts[8].X += pnts[8].X - pnts[2].X; pnts[8].Y = pnts[2].Y; // mirror point
pnts[9].X += pnts[9].X - pnts[1].X; pnts[9].Y = pnts[1].Y; // mirror point
return pnts;
}
}
public class Program //main program
{
[STAThread] // thanks adatapost!!!
public static int Main()
{
Application.Run(new DrawingForm()); // run a form
return 0; // let the OS know everything is OK
}
}
}
```

gulbano
0
Posting Whiz

ddanbe
2,724
Professional Procrastinator
Featured Poster

gulbano
0
Posting Whiz

ddanbe
2,724
Professional Procrastinator
Featured Poster

sandeepparekh9
109
Posting Whiz

robertocsa
0
Newbie Poster

ddanbe
2,724
Professional Procrastinator
Featured Poster

TnTinMN
418
Practically a Master Poster

ddanbe
2,724
Professional Procrastinator
Featured Poster

Vishwanath Reddy
0
Newbie Poster

ddanbe
2,724
Professional Procrastinator
Featured Poster

Vishwanath Reddy
0
Newbie Poster

ddanbe
2,724
Professional Procrastinator
Featured Poster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.