■ 시어핀스키 곡선(Sierpinski Curve) 그리기

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


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 Bitmap bitmap;

 

        /// <summary>

        /// 새로 고침 여부

        /// </summary>

        private bool refresh;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

        }

 

        #endregion

 

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

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

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

 

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void drawButton_Click(object sender, EventArgs e)

        {

            int depth = int.Parse(this.depthTextBox.Text);

 

            if(depth > 8)

            {

                DialogResult result = MessageBox.Show

                (

                    "깊이가 크면 그리는 시간이 오래 걸릴 수 있습니다. 계속 하시겠습니까?",

                    "확인",

                    MessageBoxButtons.YesNo,

                    MessageBoxIcon.Question

                );

 

                if(result == DialogResult.No)

                {

                    return;

                }

            }

 

            Cursor = Cursors.WaitCursor;

 

            Application.DoEvents();

 

            this.refresh = this.refreshCheckBox.Checked;

 

            this.bitmap = new Bitmap(this.canvasPictureBox.ClientSize.Width, this.canvasPictureBox.ClientSize.Height);

 

            this.canvasPictureBox.Image = this.bitmap;

 

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

            {

                graphics.Clear(this.canvasPictureBox.BackColor);

 

                float deltaX = (float)(this.bitmap.Width  / Math.Pow(2, depth - 1) / 8);

                float deltaY = (float)(this.bitmap.Height / Math.Pow(2, depth - 1) / 8);

 

                DrawSierpinskiCurve(graphics, depth, deltaX, deltaY);

            }

 

            this.canvasPictureBox.Refresh();

 

            Cursor = Cursors.Default;

        }

 

        #endregion

 

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

 

        #region 시어핀스키 곡선 A 그리기 - DrawSierpinskiCurveA(graphics, depth, deltaX, deltaY, x, y)

 

        /// <summary>

        /// 시어핀스키 곡선 A 그리기

        /// </summary>

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

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

        /// <param name="deltaX">델타 X</param>

        /// <param name="deltaY">델타 Y</param>

        /// <param name="x">X</param>

        /// <param name="y">Y</param>

        private void DrawSierpinskiCurveA(Graphics graphics, float depth, float deltaX, float deltaY, ref float x, ref float y)

        {

            if(depth > 0)

            {

                depth--;

 

                DrawSierpinskiCurveA(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, deltaX, deltaY);

 

                DrawSierpinskiCurveB(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, 2 * deltaX, 0);

 

                DrawSierpinskiCurveD(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, deltaX, -deltaY);

 

                DrawSierpinskiCurveA(graphics, depth, deltaX, deltaY, ref x, ref y);

            }

 

            if(this.refresh)

            {

                this.canvasPictureBox.Refresh();

            }

        }

 

        #endregion

        #region 시어핀스키 곡선 B 그리기 - DrawSierpinskiCurveB(graphics, depth, deltaX, deltaY, x, y)

 

        /// <summary>

        /// 시어핀스키 곡선 B 그리기

        /// </summary>

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

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

        /// <param name="deltaX">델타 X</param>

        /// <param name="deltaY">델타 Y</param>

        /// <param name="x">X</param>

        /// <param name="y">Y</param>

        private void DrawSierpinskiCurveB(Graphics graphics, float depth, float deltaX, float deltaY, ref float x, ref float y)

        {

            if(depth > 0)

            {

                depth--;

 

                DrawSierpinskiCurveB(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, -deltaX, deltaY);

 

                DrawSierpinskiCurveC(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, 0, 2 * deltaY);

 

                DrawSierpinskiCurveA(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, deltaX, deltaY);

 

                DrawSierpinskiCurveB(graphics, depth, deltaX, deltaY, ref x, ref y);

            }

 

            if(this.refresh)

            {

                this.canvasPictureBox.Refresh();

            }

        }

 

        #endregion

        #region 시어핀스키 곡선 C 그리기 - DrawSierpinskiCurveC(graphics, depth, deltaX, deltaY, x, y)

 

        /// <summary>

        /// 시어핀스키 곡선 C 그리기

        /// </summary>

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

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

        /// <param name="deltaX">델타 X</param>

        /// <param name="deltaY">델타 Y</param>

        /// <param name="x">X</param>

        /// <param name="y">Y</param>

        private void DrawSierpinskiCurveC(Graphics graphics, float depth, float deltaX, float deltaY, ref float x, ref float y)

        {

            if(depth > 0)

            {

                depth--;

 

                DrawSierpinskiCurveC(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, -deltaX, -deltaY);

 

                DrawSierpinskiCurveD(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, -2 * deltaX, 0);

 

                DrawSierpinskiCurveB(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, -deltaX, deltaY);

 

                DrawSierpinskiCurveC(graphics, depth, deltaX, deltaY, ref x, ref y);

            }

 

            if(this.refresh)

            {

                this.canvasPictureBox.Refresh();

            }

        }

 

        #endregion

        #region 시어핀스키 곡선 D 그리기 - DrawSierpinskiCurveD(graphics, depth, deltaX, deltaY, x, y)

 

        /// <summary>

        /// 시어핀스키 곡선 D 그리기

        /// </summary>

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

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

        /// <param name="deltaX">델타 X</param>

        /// <param name="deltaY">델타 Y</param>

        /// <param name="x">X</param>

        /// <param name="y">Y</param>

        private void DrawSierpinskiCurveD(Graphics graphics, float depth, float deltaX, float deltaY, ref float x, ref float y)

        {

            if(depth > 0)

            {

                depth--;

 

                DrawSierpinskiCurveD(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, deltaX, -deltaY);

 

                DrawSierpinskiCurveA(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, 0, -2 * deltaY);

 

                DrawSierpinskiCurveC(graphics, depth, deltaX, deltaY, ref x, ref y);

 

                DrawLine(graphics, ref x, ref y, -deltaX, -deltaY);

 

                DrawSierpinskiCurveD(graphics, depth, deltaX, deltaY, ref x, ref y);

            }

 

            if(this.refresh)

            {

                this.canvasPictureBox.Refresh();

            }

        }

 

        #endregion

        #region 선 그리기 - DrawLine(graphics, depth, deltaX, deltaY, x, y)

 

        /// <summary>

        /// 선 그리기

        /// </summary>

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

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

        /// <param name="deltaX">델타 X</param>

        /// <param name="deltaY">델타 Y</param>

        /// <param name="x">X</param>

        /// <param name="y">Y</param>

        private void DrawLine(Graphics graphics, ref float x, ref float y, float deltaX, float deltaY)

        {

            graphics.DrawLine(Pens.Black, x, y, x + deltaX, y + deltaY);

 

            x += deltaX;

            y += deltaY;

        }

 

        #endregion

        #region 시어핀스키 곡선 그리기 - DrawSierpinskiCurve(graphics, depth, deltaX, deltaY)

 

        /// <summary>

        /// 시어핀스키 곡선 그리기

        /// </summary>

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

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

        /// <param name="deltaX">델타 X</param>

        /// <param name="deltaY">델타 Y</param>

        private void DrawSierpinskiCurve(Graphics graphics, int depth, float deltaX, float deltaY)

        {

            float x = 2 * deltaX;

            float y = deltaY;

 

            DrawSierpinskiCurveA(graphics, depth, deltaX, deltaY, ref x, ref y);

 

            DrawLine(graphics, ref x, ref y, deltaX, deltaY);

 

            DrawSierpinskiCurveB(graphics, depth, deltaX, deltaY, ref x, ref y);

 

            DrawLine(graphics, ref x, ref y, -deltaX, deltaY);

 

            DrawSierpinskiCurveC(graphics, depth, deltaX, deltaY, ref x, ref y);

 

            DrawLine(graphics, ref x, ref y, -deltaX, -deltaY);

 

            DrawSierpinskiCurveD(graphics, depth, deltaX, deltaY, ref x, ref y);

 

            DrawLine(graphics, ref x, ref y, deltaX, -deltaY);

 

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요