Mega Code Archive

 
Categories / C# / 2D Graphics
 

DrawPie with offset

using System; using System.Drawing; using System.Windows.Forms;     class PieChart: Form {      int[] aiValues = { 50, 100, 25, 150, 100, 75 };          public static void Main()      {           Application.Run(new PieChart());      }      public PieChart()      {           ResizeRedraw = true;      }      protected override void OnPaint(PaintEventArgs pea)      {           DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);      }      protected void DoPage(Graphics grfx, Color clr, int cx, int cy)      {           Rectangle rect   = new Rectangle(50, 50, 200, 200);           Pen       pen    = new Pen(clr);           int       iTotal = 0;           float     fAngle = 0, fSweep;               foreach(int iValue in aiValues)                iTotal += iValue;               foreach(int iValue in aiValues)           {                fSweep = 360f * iValue / iTotal;                DrawPieSlice(grfx, pen, rect, fAngle, fSweep);                fAngle += fSweep;           }      }      protected virtual void DrawPieSlice(Graphics grfx, Pen pen,                                           Rectangle rect,                                          float fAngle, float fSweep)      {           float fSlice = (float)(2 * Math.PI * (fAngle + fSweep / 2) / 360);               rect.Offset((int)(rect.Width  / 10 * Math.Cos(fSlice)),                       (int)(rect.Height / 10 * Math.Sin(fSlice)));                                  grfx.DrawPie(pen, rect, fAngle, fSweep);       } }