954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Draw any star you want.

0
By Marivoet Daniel on Apr 17th, 2011 8:21 pm

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

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
        }
    }
}

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

gulbano
Posting Whiz
336 posts since Apr 2011
Reputation Points: 11
Solved Threads: 2
 

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

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Thanks ddanbe :) it was the problem Nice code

gulbano
Posting Whiz
336 posts since Apr 2011
Reputation Points: 11
Solved Threads: 2
 

You're welcome, have fun! :)

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

nice

sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: