using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DrawPie_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            #if DEBUG
                MinimizeBox = false;
            #else       
                MinimizeBox = true;
            #endif
        }

        ///<summary>
        ///Рисуем закрашенный сектор
        ///Параметры функции
        ///g              - Объект Graphics
        ///solidBrush     - Кисть для закраски сегмента
        ///x,y            - Координаты центра
        ///width          - Ширина сегмента
        ///height         - Высота сегмента
        ///startAngle     - Значение начального угла
        ///endAngle       - Значение конечного угла
        ///</summary>
        private void FillPie(Graphics g, SolidBrush solidBrush,
                                int x, int y, int width, int height,
                                double startAngle, double endAngle)
        {
            double[] xAngle = new double[12];
            double[] yAngle = new double[12];
            double angleIncrement = (endAngle - startAngle) / 10;
            double angle = startAngle;

            for (int i = 0; i <= 10; i++)
            {
                xAngle[i] = x + (Math.Cos(angle * (Math.PI / 180))
                                * (width / 2));
                yAngle[i] = y + (Math.Sin(angle * (Math.PI / 180))
                                * (height / 2));
                angle += angleIncrement;
            }
            xAngle[11] = x + (Math.Cos(endAngle * (Math.PI / 180))
                            * (width / 2));
            yAngle[11] = y + (Math.Sin(endAngle * (Math.PI / 180))
                            * (height / 2));

            Point[] anglePoints =
                {
                    new Point(x, y),
                    new Point((int)xAngle[0], (int)yAngle[0]),
                    new Point((int)xAngle[1], (int)yAngle[1]),
                    new Point((int)xAngle[2], (int)yAngle[2]),
                    new Point((int)xAngle[3], (int)yAngle[3]),
                    new Point((int)xAngle[4], (int)yAngle[4]),
                    new Point((int)xAngle[5], (int)yAngle[5]),
                    new Point((int)xAngle[6], (int)yAngle[6]),
                    new Point((int)xAngle[7], (int)yAngle[7]),
                    new Point((int)xAngle[8], (int)yAngle[8]),
                    new Point((int)xAngle[9], (int)yAngle[9]),
                    new Point((int)xAngle[10], (int)yAngle[10]),
                    new Point((int)xAngle[11], (int)yAngle[11])
                };

            g.FillPolygon(solidBrush, anglePoints);
        }

        ///<summary>
        /// Рисуем границы сектора
        ///g              - Объект Graphics 
        ///pen            - Перо для рисования сегмента 
        ///x,y            - Центр сегмента 
        ///width          - Ширина пирогоа 
        ///height         - Высота 
        ///startAngle     - Значение начального угла 
        ///endAngle       - Значение конечного угла 
        ///</summary>
        
       private void DrawPie(Graphics g, Pen pen,
                                int x, int y, int width, int height,
                                double startAngle, double endAngle)
        {
            double[] xAngle = new double[12]; 
            double[] yAngle = new double[12];
            double angleIncrement = (endAngle - startAngle) / 10;
            double angle = startAngle;

            for (int i = 0; i <= 10; i++)
            {
                xAngle[i] = x + (Math.Cos(angle * (Math.PI / 180))
                                * (width / 2));
                yAngle[i] = y + (Math.Sin(angle * (Math.PI / 180))
                                * (height / 2));
                angle += angleIncrement;
            }
            xAngle[11] = x + (Math.Cos(endAngle * (Math.PI / 180))
                            * (width / 2));
            yAngle[11] = y + (Math.Sin(endAngle * (Math.PI / 180))
                            * (height / 2));

            Point[] anglePoints =
            {
                new Point(x,y),
                new Point((int)xAngle[0],(int)yAngle[0]),
                new Point((int)xAngle[1],(int)yAngle[1]),
                new Point((int)xAngle[2],(int)yAngle[2]),
                new Point((int)xAngle[3],(int)yAngle[3]),
                new Point((int)xAngle[4],(int)yAngle[4]),
                new Point((int)xAngle[5],(int)yAngle[5]),
                new Point((int)xAngle[6],(int)yAngle[6]),
                new Point((int)xAngle[7],(int)yAngle[7]),
                new Point((int)xAngle[8],(int)yAngle[8]),
                new Point((int)xAngle[9],(int)yAngle[9]),
                new Point((int)xAngle[10],(int)yAngle[10]),
                new Point((int)xAngle[11],(int)yAngle[11])
            };

            g.DrawPolygon(pen, anglePoints);
        }

        
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Выводим несколько секторов на экран
            DrawPie(e.Graphics, new Pen(Color.Red), 130, 165, 100, 100, 0, 45);
            FillPie(e.Graphics, new SolidBrush(Color.Green), 120, 160, 100, 100, 46, 90);
            FillPie(e.Graphics, new SolidBrush(Color.Yellow), 120, 160, 100, 100, 91, 120);
            FillPie(e.Graphics, new SolidBrush(Color.Blue), 120, 160, 100, 100, 121, 260);
            FillPie(e.Graphics, new SolidBrush(Color.Red), 120, 160, 100, 100, 261, 360);
        }
    }
}