■ 오각형(Pentagram) 그리기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 파란색 펜

        /// </summary>

        private Pen bluePen;

 

        /// <summary>

        /// 빨간색 펜

        /// </summary>

        private Pen redPen;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.bluePen = new Pen(Brushes.Blue);

            this.redPen  = new Pen(Brushes.Red );

 

            SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

 

            UpdateStyles();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 배경 페인트시 처리하기 - OnPaintBackground(e)

 

        /// <summary>

        /// 배경 페인트시 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected override void OnPaintBackground(PaintEventArgs e)

        {

            e.Graphics.Clear(Color.White);

        }

 

        #endregion

        #region 페인트시 처리하기 - OnPaint(e)

 

        /// <summary>

        /// 페인트시 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected override void OnPaint(PaintEventArgs e)

        {

            DrawPentagram(e.Graphics);

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 라디안 구하기 - GetRadian(degree)

 

        /// <summary>

        /// 라디안 구하기

        /// </summary>

        /// <param name="degree"></param>

        /// <returns>라디안</returns>

        private double GetRadian(double degree)

        {

            return Math.PI * degree / 180.0;

        }

 

        #endregion

        #region 원 그리기 - DrawCircle(graphics, pen, centerX, centerY, radius)

 

        /// <summary>

        /// 원 그리기

        /// </summary>

        /// <param name="graphics">그래픽스</param>

        /// <param name="pen"></param>

        /// <param name="centerX">중심 X 좌표</param>

        /// <param name="centerY">중심 Y 좌표</param>

        /// <param name="radius">반경</param>

        private void DrawCircle(Graphics graphics, Pen pen, int centerX, int centerY, int radius)

        {

            Rectangle rectangle = new Rectangle(centerX - radius, centerY - radius, radius * 2, radius * 2);

 

            graphics.DrawEllipse(pen, rectangle);

        }

 

        #endregion

        #region 오각형 그리기 - DrawPentagram(graphics, pointArray)

 

        /// <summary>

        /// 오각형 그리기

        /// </summary>

        /// <param name="graphics">그래픽스</param>

        /// <param name="pointArray">포인트 배열</param>

        private void DrawPentagram(Graphics graphics, Point[] pointArray)

        {

            Point startPoint = pointArray[0];

 

            int offset = 2;

 

            for(int i = 0; i < pointArray.Length; i++)

            {

                Point endPoint = pointArray[offset % 5];

 

                graphics.DrawLine(Pens.Red, startPoint, endPoint);

 

                startPoint = endPoint;

 

                offset += 2;

            }

        }

 

        #endregion

        #region 오각형 그리기 - DrawPentagram(graphics)

 

        /// <summary>

        /// 오각형 그리기

        /// </summary>

        /// <param name="graphics">그래픽스</param>

        private void DrawPentagram(Graphics graphics)

        {

            Point[] pointArray = new Point[5];

 

            int centerX = Width  / 2;

            int centerY = Height / 2;

 

            int radius = 200;

 

            DrawCircle(graphics, this.bluePen, centerX, centerY, radius);

 

            DrawCircle(graphics, this.redPen, centerX, centerY - radius, 10);

 

            double yPosition = Math.Cos(GetRadian(72)) * radius;

 

            int lastDegree = 90 - 72;

 

            double xPosition = Math.Cos(GetRadian(lastDegree)) * radius;

 

            DrawCircle(graphics, this.redPen, centerX - (int)xPosition, centerY - (int)yPosition, 10);

 

            pointArray[0] = new Point(centerX, centerY - radius);

            pointArray[1] = new Point(centerX - (int)xPosition, centerY - (int)yPosition);

 

            graphics.DrawLine(redPen, pointArray[0], pointArray[1]);

 

            xPosition = Math.Cos(GetRadian(72 - lastDegree)) * radius;

 

            lastDegree = 90 - (72 - lastDegree);

 

            yPosition = Math.Cos(GetRadian(lastDegree)) * radius;

 

            DrawCircle(graphics, redPen, centerX - (int)xPosition, centerY + (int)yPosition, 10);

 

            pointArray[2] = new Point(centerX - (int)xPosition, centerY + (int)yPosition);

 

            graphics.DrawLine(redPen, pointArray[1], pointArray[2]);

 

            yPosition = Math.Cos(GetRadian(72 - lastDegree)) * radius;

 

            xPosition = Math.Sin(GetRadian(lastDegree)) * radius;

 

            DrawCircle(graphics, redPen, centerX + (int)xPosition, centerY + (int)yPosition, 10);

 

            pointArray[3] = new Point(centerX + (int)xPosition, centerY + (int)yPosition);

 

            graphics.DrawLine(redPen, pointArray[2], pointArray[3]);

 

            lastDegree = 72 - (90 - lastDegree);

 

            xPosition = Math.Cos(GetRadian(lastDegree)) * radius;

            yPosition = Math.Sin(GetRadian(lastDegree)) * radius;

 

            DrawCircle(graphics, redPen, centerX + (int)xPosition, centerY - (int)yPosition, 10);

 

            pointArray[4] = new Point(centerX + (int)xPosition, centerY - (int)yPosition);

 

            graphics.DrawLine(redPen, pointArray[3], pointArray[4]);

 

            graphics.DrawLine(redPen, pointArray[4], pointArray[0]);

 

            DrawPentagram(graphics, pointArray);

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요