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

■ 시어핀스키 카펫(Sierpinski Carpet) 그리기

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


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)

        {

            DrawSierpinskiCarpet();

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void Form_Resize(object sender, EventArgs e)

        {

            DrawSierpinskiCarpet();

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void drawButton_Click(object sender, EventArgs e)

        {

            DrawSierpinskiCarpet();

        }

 

        #endregion

 

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

 

        #region 사각형 그리기 - DrawRectangle(graphics, level, rectangle)

 

        /// <summary>

        /// 사각형 그리기

        /// </summary>

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

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

        /// <param name="rectangle">사각형</param>

        private void DrawRectangle(Graphics graphics, int level, RectangleF rectangle)

        {

            if(level == 0)

            {

                graphics.FillRectangle(Brushes.Blue, rectangle);

            }

            else

            {

                float width = rectangle.Width / 3f;

                float x0    = rectangle.Left;

                float x1    = x0 + width;

                float x2    = x0 + width * 2f;

 

                float height = rectangle.Height / 3f;

                float y0     = rectangle.Top;

                float y1     = y0 + height;

                float y2     = y0 + height * 2f;

 

                DrawRectangle(graphics, level - 1, new RectangleF(x0, y0, width, height));

                DrawRectangle(graphics, level - 1, new RectangleF(x1, y0, width, height));

                DrawRectangle(graphics, level - 1, new RectangleF(x2, y0, width, height));

                DrawRectangle(graphics, level - 1, new RectangleF(x0, y1, width, height));

                DrawRectangle(graphics, level - 1, new RectangleF(x2, y1, width, height));

                DrawRectangle(graphics, level - 1, new RectangleF(x0, y2, width, height));

                DrawRectangle(graphics, level - 1, new RectangleF(x1, y2, width, height));

                DrawRectangle(graphics, level - 1, new RectangleF(x2, y2, width, height));

            }

        }

 

        #endregion

        #region 시어핀스키 카펫 그리기 - DrawSierpinskiCarpet()

 

        /// <summary>

        /// 시어핀스키 카펫 그리기

        /// </summary>

        private void DrawSierpinskiCarpet()

        {

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

 

            Bitmap bitmap = new Bitmap

            (

                this.cancasPictureBox.ClientSize.Width,

                this.cancasPictureBox.ClientSize.Height

            );

 

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

            {

                graphics.Clear(Color.White);

 

                graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

                const float MARGIN = 10;

 

                RectangleF rectangle = new RectangleF

                (

                    MARGIN,

                    MARGIN,

                    this.cancasPictureBox.ClientSize.Width  - 2 * MARGIN,

                    this.cancasPictureBox.ClientSize.Height - 2 * MARGIN

                );

 

                DrawRectangle(graphics, this.level, rectangle);

            }

 

            this.cancasPictureBox.Image = bitmap;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요