Draw any star you want.

ddanbe 1 Tallied Votes 3K Views Share

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

TnTinMN commented: fun +6
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Draw
    public class DrawingForm : Form
        public DrawingForm() // contructor
            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

I have tried this code but getting error on LinearGradientBrush, HatchBrush, SmoothingMode .... Can you please clear me how to use them i have googled them but could not find help

ddanbe 2,724 Professional Procrastinator Featured Poster

Did you include using System.Drawing.Drawing2D;?
Those methods allcome out of the Drawing2D namespace.
Hope it helps.

gulbano 0 Posting Whiz

Thanks ddanbe :) it was the problem Nice code

ddanbe 2,724 Professional Procrastinator Featured Poster

You're welcome, have fun! :)

sandeepparekh9 109 Posting Whiz


robertocsa 0 Newbie Poster

Very welldone! Congratulations!

ddanbe 2,724 Professional Procrastinator Featured Poster

Thank you robertocsa, welcome to the daniweb community! Feel free to use this code to your liking.

TnTinMN 418 Practically a Master Poster

Hi ddanbe,

Fun little program. It reminded me how much fun I had drawing geometric shapes back in the dinosaur days when PC's were a curiousity to most.
It inspired me to see how much I have forgotten. :) It took a while but is finally gelled in my mind I was back in the groove with polar to cartesian transformations. If your interested, here is my attempt. It will draw multipointed stars.

ddanbe 2,724 Professional Procrastinator Featured Poster

@TnTinMN: Thanks for sharing your code!

Vishwanath Reddy 0 Newbie Poster

hi ddanbe,

Thank you for such good article.
This artcle talks about 5 points star can be this be generalised for n number of points(like 3 to 15 or 3-30) where we can draw n nodes star.

Based on your code I have tried to do but was unable to do. I done for 3, 4, 6 stars based on youd article.

Can ypu please help me out how to write a generalised trig for n points star.

Thanks & Regards,

ddanbe 2,724 Professional Procrastinator Featured Poster

Thanks Vishwanath Reddy, for liking my snippet.
Yes, I know it is a mjor flaw of my code that is not general at all.
So indeed "Draw any star you want is an overstatement.
But for an N pointed star, you would need 2N PointF and the angle between two starpoint would be 360/N and the inner points would be just in between 360/2N. Your sine and cosine calculations of the points would of course be done in a loop. Hope it helps a bit.

Vishwanath Reddy 0 Newbie Poster

Thank you Dani for quick reply. I will work around as you suggested.
Yes it helps me a lot.

ddanbe 2,724 Professional Procrastinator Featured Poster

Hey! Do I look so much like Dani? My avatar is me, with the reds en the yellows, photoshopped away. And it was done before "Avatar"!

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.