■ 헤이웨이 드래곤 프랙탈(Heighway Dragon Fractal) 그리기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration

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

 

        #region 방향 - Direction

 

        /// <summary>

        /// 방향

        /// </summary>

        private enum Direction

        {

            /// <summary>

            /// 왼쪽

            /// </summary>

            Left,

 

            /// <summary>

            /// 오른쪽

            /// </summary>

            Right

        }

 

        #endregion

 

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

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

 

        #region Field

 

        /// <summary>

        /// 마지막 그리기 색상

        /// </summary>

        private Color drawLastColor = Color.Black;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                              += Form_Load;

            this.drawButton.Click             += drawButton_Click;

            this.canvasPictureBox.SizeChanged += canvasPictureBox_SizeChanged;

            this.canvasPictureBox.MouseClick  += canvasPictureBox_MouseClick;

 

            #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)

        {

            Draw();

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void drawButton_Click(object sender, EventArgs e)

        {

            Draw();

        }

 

        #endregion

        #region 캔버스 픽처 박스 크기 변경시 처리하기 - canvasPictureBox_SizeChanged(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 크기 변경시 처리하기

        /// </summary>

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

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

        private void canvasPictureBox_SizeChanged(object sender, EventArgs e)

        {

            Draw();

        }

 

        #endregion

        #region 캔버스 픽처 박스 마우스 클릭시 처리하기 - canvasPictureBox_MouseClick(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 마우스 클릭시 처리하기

        /// </summary>

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

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

        private void canvasPictureBox_MouseClick(object sender, MouseEventArgs e)

        {

            using(Bitmap bitmap = new Bitmap(this.canvasPictureBox.Image))

            {

                Color color = bitmap.GetPixel(e.X, e.Y);

 

                if(color == Color.FromArgb(255, 255, 0, 0))

                {

                    this.drawLastColor = Color.Red;

                }

                else if(color == Color.FromArgb(255, 0, 255, 0))

                {

                    this.drawLastColor = Color.Green;

                }

                else if(color == Color.FromArgb(255, 0, 0, 255))

                {

                    this.drawLastColor = Color.Blue;

                }

                else if(color == Color.FromArgb(255, 0, 0, 0))

                {

                    this.drawLastColor = Color.Black;

                }

            }

 

            Draw();

        }

 

        #endregion

 

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

 

        #region 헤이웨이 드래곤 프랙탈 그리기 - DrawHeighwayDragonFractal(graphics, pen, level, turnDirection, x1, y1, dx, dy)

 

        /// <summary>

        /// 헤이웨이 드래곤 프랙탈 그리기

        /// </summary>

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

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

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

        /// <param name="turnDirection">전환 방향</param>

        /// <param name="x1">X1</param>

        /// <param name="y1">Y1</param>

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

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

        private void DrawHeighwayDragonFractal(Graphics graphics, Pen pen, int level, Direction turnDirection,

            float x1, float y1, float dx, float dy)

        {

            if(level <= 0)

            {

                graphics.DrawLine(pen, x1, y1, x1 + dx, y1 + dy);

            }

            else

            {

                float nx = (float)(dx / 2);

                float ny = (float)(dy / 2);

 

                float dx2 = -ny + nx;

                float dy2 = nx + ny;

 

                if(turnDirection == Direction.Right)

                {

                    DrawHeighwayDragonFractal(graphics, pen, level - 1, Direction.Right, x1, y1, dx2, dy2);

 

                    float x2 = x1 + dx2;

                    float y2 = y1 + dy2;

 

                    DrawHeighwayDragonFractal(graphics, pen, level - 1, Direction.Left, x2, y2, dy2, -dx2);

                }

                else

                {

                    DrawHeighwayDragonFractal(graphics, pen, level - 1, Direction.Right, x1, y1, dy2, -dx2);

 

                    float x2 = x1 + dy2;

                    float y2 = y1 - dx2;

 

                    DrawHeighwayDragonFractal(graphics, pen, level - 1, Direction.Left, x2, y2, dx2, dy2);

                }

            }

        }

 

        #endregion

        #region 그리기 - Draw()

 

        /// <summary>

        /// 그리기

        /// </summary>

        private void Draw()

        {

            Cursor = Cursors.WaitCursor;

 

            Bitmap bitmap = new Bitmap

            (

                this.canvasPictureBox.ClientSize.Width,

                this.canvasPictureBox.ClientSize.Height

            );

 

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

            {

                graphics.Clear(canvasPictureBox.BackColor);

 

                const int MARGIN = 5;

 

                float dx = Math.Min

                (

                    (this.canvasPictureBox.ClientSize.Width  - 2 * MARGIN) / (14 / 6f),

                    (this.canvasPictureBox.ClientSize.Height - 2 * MARGIN) / (14 / 6f)

                );

 

                float x0 = this.canvasPictureBox.ClientSize.Width  / 2;

                float y0 = this.canvasPictureBox.ClientSize.Height / 2;

 

                int level = (int)this.levelNumericUpDown.Value;

 

                if(this.drawLastColor != Color.Red)

                {

                    DrawHeighwayDragonFractal(graphics, Pens.Red, level, Direction.Right, x0, y0, dx, 0);

                }

 

                if(this.drawLastColor != Color.Green)

                {

                    DrawHeighwayDragonFractal(graphics, Pens.Green, level, Direction.Right, x0, y0, 0, dx);

                }

 

                if(this.drawLastColor != Color.Blue)

                {

                    DrawHeighwayDragonFractal(graphics, Pens.Blue, level, Direction.Right, x0, y0, -dx, 0);

                }

 

                if(this.drawLastColor != Color.Black)

                {

                    DrawHeighwayDragonFractal(graphics, Pens.Black, level, Direction.Right, x0, y0, 0, -dx);

                }

 

                if(this.drawLastColor == Color.Red)

                {

                    DrawHeighwayDragonFractal(graphics, Pens.Red, level, Direction.Right, x0, y0, dx, 0);

                }

                else if(this.drawLastColor == Color.Green)

                {

                    DrawHeighwayDragonFractal(graphics, Pens.Green, level, Direction.Right, x0, y0, 0, dx);

                }

                else if(this.drawLastColor == Color.Blue)

                {

                    DrawHeighwayDragonFractal(graphics, Pens.Blue, level, Direction.Right, x0, y0, -dx, 0);

                }

                else if(this.drawLastColor == Color.Black)

                {

                    DrawHeighwayDragonFractal(graphics, Pens.Black, level, Direction.Right, x0, y0, 0, -dx);

                }

            }

 

            this.canvasPictureBox.Image = bitmap;

 

            Cursor = Cursors.Default;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요