■ 시어핀스키 펜타곤(Sierpinski Pentagon) 그리기

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


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 float sizeScale = (float)(1.0 / (2.0 * (1 + Math.Cos(Math.PI / 180 * 72))));

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.depthNumericUpDown.ValueChanged += depthNumericUpDown_ValueChanged;

            this.canvasPictureBox.Paint          += canvasPictureBox_Paint;

            this.canvasPictureBox.Resize         += canvasPictureBox_Resize;

 

            #endregion

        }

 

        #endregion

 

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

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

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 깊이 숫자 UP/DOWN 값 변경시 처리하기 - depthNumericUpDown_ValueChanged(sender, e)

 

        /// <summary>

        /// 깊이 숫자 UP/DOWN 값 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

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

        private void depthNumericUpDown_ValueChanged(object sender, EventArgs e)

        {

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

        #region 캔버스 픽처 박스 크기 조정시 처리하기 - canvasPictureBox_Resize(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 크기 조정시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

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

        private void canvasPictureBox_Resize(object sender, EventArgs e)

        {

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

        #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 페인트시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

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

        private void canvasPictureBox_Paint(object sender, PaintEventArgs e)

        {

            int depth = (int)this.depthNumericUpDown.Value;

 

            PointF centerPoint = new PointF

            (

                this.canvasPictureBox.ClientSize.Width  / 2,

                this.canvasPictureBox.ClientSize.Height / 2

            );

 

            float radius = (float)Math.Min(centerPoint.X,  centerPoint.Y);

 

            DrawSierpinskiPentagon(depth, e.Graphics, Brushes.Red, centerPoint, radius);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 시어핀스키 펜타곤 그리기 - DrawSierpinskiPentagon(depth, graphics, brush, centerPoint, radius)

 

        /// <summary>

        /// 시어핀스키 펜타곤 그리기

        /// </summary>

        /// <param name="depth">깊이</param>

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

        /// <param name="brush">브러시</param>

        /// <param name="centerPoint">중심 포인트</param>

        /// <param name="radius">반지름</param>

        private void DrawSierpinskiPentagon(int depth, Graphics graphics, Brush brush, PointF centerPoint, float radius)

        {

            if(depth <= 0)

            {

                PointF[] pointArray = GetPentagonPointArray(centerPoint, radius);

 

                graphics.FillPolygon(brush, pointArray);

            }

            else

            {

                float d = radius - radius * this.sizeScale;

 

                PointF[] centerPointArray = GetPentagonPointArray(centerPoint, d);

 

                foreach(PointF point in centerPointArray)

                {

                    DrawSierpinskiPentagon(depth - 1, graphics, brush, point, radius * this.sizeScale);

                }

            }

        }

 

        #endregion

        #region 펜타곤 포인트 배열 구하기 - GetPentagonPointArray(centerPoint, radius)

 

        /// <summary>

        /// 펜타곤 포인트 배열 구하기

        /// </summary>

        /// <param name="centerPoint">중심 포인트</param>

        /// <param name="radius">반지름</param>

        /// <returns>펜타곤 포인트 배열</returns>

        private PointF[] GetPentagonPointArray(PointF centerPoint, float radius)

        {

            PointF[] pointArray = new PointF[5];

 

            double theta  = -Math.PI / 2.0;

            double dtheta = 2.0 * Math.PI / 5.0;

 

            for(int i = 0; i < 5; i++)

            {

                pointArray[i] = new PointF

                (

                    centerPoint.X + (float)(radius * Math.Cos(theta)),

                    centerPoint.Y + (float)(radius * Math.Sin(theta))

                );

 

                theta += dtheta;

            }

 

            return pointArray;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요