첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 시어핀스키 가스켓(Sierpinski Gasket) 그리기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 레벨

        /// </summary>

        private int level = 5;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                  += Form_Load;

            Resize                += Form_Resize;

            this.drawButton.Click += drawButton_Click;

 

            #endregion

        }

 

        #endregion

 

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

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

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

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

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

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

        private void Form_Load(object sender, EventArgs e)

        {

            DrawSierpinskiGasket();

        }

 

        #endregion

        #region 폼 크기 조정시 처리하기 - Form_Resize(sender, e)

 

        /// <summary>

        /// 폼 크기 조정시 처리하기

        /// </summary>

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

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

        private void Form_Resize(object sender, EventArgs e)

        {

            DrawSierpinskiGasket();

        }

 

        #endregion

        #region 그리기 버튼 클릭시 처리하기 - drawButton_Click(sender, e)

 

        /// <summary>

        /// 그리기 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void drawButton_Click(object sender, EventArgs e)

        {

            DrawSierpinskiGasket();

        }

 

        #endregion

 

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

 

        #region 삼각형 그리기 - DrawTriangle(graphics, level, topPoint, leftPoint, rightPoint)

 

        /// <summary>

        /// 삼각형 그리기

        /// </summary>

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

        /// <param name="level">레벨</param>

        /// <param name="topPoint">위쪽 포인트</param>

        /// <param name="leftPoint">왼쪽 포인트</param>

        /// <param name="rightPoint">아래쪽 포인트</param>

        private void DrawTriangle(Graphics graphics, int level, PointF topPoint, PointF leftPoint, PointF rightPoint)

        {

            if(level == 0)

            {

                PointF[] pointArray = { topPoint, rightPoint, leftPoint };

 

                graphics.FillPolygon(Brushes.Red, pointArray);

            }

            else

            {

                PointF leftMiddle = new PointF

                (

                    (topPoint.X + leftPoint.X) / 2f,

                    (topPoint.Y + leftPoint.Y) / 2f

                );

 

                PointF rightMiddle = new PointF

                (

                    (topPoint.X + rightPoint.X) / 2f,

                    (topPoint.Y + rightPoint.Y) / 2f

                );

 

                PointF bottomModdle = new PointF

                (

                    (leftPoint.X + rightPoint.X) / 2f,

                    (leftPoint.Y + rightPoint.Y) / 2f

                );

 

                DrawTriangle(graphics, level - 1, topPoint   , leftMiddle  , rightMiddle );

                DrawTriangle(graphics, level - 1, leftMiddle , leftPoint   , bottomModdle);

                DrawTriangle(graphics, level - 1, rightMiddle, bottomModdle, rightPoint  );

            }

        }

 

        #endregion

        #region 시어핀스키 가스켓 그리기 - DrawSierpinskiGasket()

 

        /// <summary>

        /// 시어핀스키 가스켓 그리기

        /// </summary>

        private void DrawSierpinskiGasket()

        {

            this.level = int.Parse(levelTextBox.Text);

 

            Bitmap bitmap = new Bitmap

            (

                this.canvasPictureBox.ClientSize.Width,

                this.canvasPictureBox.ClientSize.Height

            );

 

            using(Graphics graphics = Graphics.FromImage(bitmap))

            {

                graphics.Clear(Color.White);

 

                graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

                const float MARGIN = 10;

 

                PointF topPoint = new PointF

                (

                    this.canvasPictureBox.ClientSize.Width / 2f,

                    MARGIN

                );

 

                PointF leftPoint = new PointF

                (

                    MARGIN,

                    this.canvasPictureBox.ClientSize.Height - MARGIN

                );

 

                PointF rightPoint = new PointF

                (

                    this.canvasPictureBox.ClientRectangle.Right  - MARGIN,

                    this.canvasPictureBox.ClientRectangle.Bottom - MARGIN

                );

 

                DrawTriangle(graphics, this.level, topPoint, leftPoint, rightPoint);

            }

 

            this.canvasPictureBox.Image = bitmap;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요