첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Class
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 가지 정보 - BranchInfo

        /// <summary>
        /// 가지 정보
        /// </summary>
        private class BranchInfo
        {
            //////////////////////////////////////////////////////////////////////////////////////////////////// Field
            ////////////////////////////////////////////////////////////////////////////////////////// Public

            #region Field

            /// <summary>
            /// X 좌표
            /// </summary>
            public float X;
            
            /// <summary>
            /// Y 좌표
            /// </summary>
            public float Y;
            
            /// <summary>
            /// 세타
            /// </summary>
            public float Theta;
            
            /// <summary>
            /// 길이
            /// </summary>
            public float Length;

            /// <summary>
            /// 깊이
            /// </summary>
            public int Depth;

            #endregion

            //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
            ////////////////////////////////////////////////////////////////////////////////////////// Public

            #region 생성자 - BranchInfo(x, y, theta, length, depth)

            /// <summary>
            /// 생성자
            /// </summary>
            /// <param name="x">X 좌표</param>
            /// <param name="y">Y 좌표</param>
            /// <param name="theta">세타</param>
            /// <param name="length">길이</param>
            /// <param name="depth">깊이</param>
            public BranchInfo(float x, float y, float theta, float length, int depth)
            {
                X      = x;
                Y      = y;
                Theta  = theta;
                Length = length;
                Depth  = depth;
            }

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 이벤트를 설정한다.

            this.depthNumericUpDown.KeyUp             += parameterControl_KeyUp;
            this.depthNumericUpDown.ValueChanged      += parameterControl_ValueChanged;
            this.lengthNumericUpDown.KeyUp            += parameterControl_KeyUp;
            this.lengthNumericUpDown.ValueChanged     += parameterControl_ValueChanged;
            this.lengthScaleTextBox.TextChanged       += parameterControl_ValueChanged;
            this.deltaThetaNumericUpDown.ValueChanged += parameterControl_ValueChanged;
            this.deltaThetaNumericUpDown.KeyUp        += parameterControl_KeyUp;
            this.canvasPictureBox.Resize              += canvasPictureBox_Resize;
            this.canvasPictureBox.Paint               += canvasPictureBox_Paint;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 매개 변수 컨트롤 키 UP 처리하기 - parameterControl_KeyUp(sender, e)

        /// <summary>
        /// 매개 변수 컨트롤 키 UP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void parameterControl_KeyUp(object sender, KeyEventArgs e)
        {
            this.canvasPictureBox.Refresh();
        }

        #endregion
        #region 매개 변수 컨트롤 값 변경시 처리하기 - parameterControl_ValueChanged(sender, e)

        /// <summary>
        /// 매개 변수 컨트롤 값 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void parameterControl_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)
        {
            e.Graphics.Clear(this.canvasPictureBox.BackColor);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            try
            {
                float rootX = this.canvasPictureBox.ClientSize.Width  / 2;
                float rootY = this.canvasPictureBox.ClientSize.Height - 4;

                float lengthScale = float.Parse(this.lengthScaleTextBox.Text);

                float deltaTheta = (float)(Math.PI / 180.0 * (double)this.deltaThetaNumericUpDown.Value);

                using(Pen pen = new Pen(Color.Black))
                {
                    DrawTree
                    (
                        e.Graphics,
                        pen,
                        (int)this.depthNumericUpDown.Value,
                        rootX,
                        rootY,
                        (int)this.lengthNumericUpDown.Value,
                        (float)(-Math.PI / 2),
                        lengthScale,
                        deltaTheta
                    );
                }
            }
            catch
            {
            }
        }

        #endregion

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

        #region 나무 그리기 - DrawTree(graphics, pen, maximumDepth, x, y, maximumLength, initialTheta, lengthScale, deltaTheta)

        /// <summary>
        /// 나무 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="pen">펜</param>
        /// <param name="maximumDepth">최대 깊이</param>
        /// <param name="x">X 좌표</param>
        /// <param name="y">Y 좌표</param>
        /// <param name="maximumLength">최대 길이</param>
        /// <param name="initialTheta">초기 세타</param>
        /// <param name="lengthScale">길이 스케일</param>
        /// <param name="deltaTheta">델타 세타</param>
        private void DrawTree(Graphics graphics, Pen pen, int maximumDepth, float x, float y, float maximumLength, float initialTheta, float lengthScale, float deltaTheta)
        {
            Queue<BranchInfo> branchInfoQueue = new Queue<BranchInfo>();

            branchInfoQueue.Enqueue(new BranchInfo(x, y, initialTheta, maximumLength, maximumDepth));

            while(branchInfoQueue.Count > 0)
            {
                BranchInfo branchInfo = branchInfoQueue.Dequeue();

                if(branchInfo.Depth == 1)
                {
                    pen.Color = Color.Red;
                }
                else
                {
                    int green = 255 * (maximumDepth - branchInfo.Depth) / maximumDepth;
                    int red   = 139 * (branchInfo.Depth - 3) / maximumDepth;

                    if(red < 0)
                    {
                        red = 0;
                    }

                    int blue = 0;

                    pen.Color = Color.FromArgb(red, green, blue);
                }

                int thickness = 10 * branchInfo.Depth / maximumDepth;

                if(thickness < 0)
                {
                    thickness = 0;
                }

                pen.Width = thickness;

                float x1 = (float)(branchInfo.X + branchInfo.Length * Math.Cos(branchInfo.Theta));
                float y1 = (float)(branchInfo.Y + branchInfo.Length * Math.Sin(branchInfo.Theta));

                graphics.DrawLine(pen, branchInfo.X, branchInfo.Y, x1, y1);

                if(branchInfo.Depth > 1)
                {
                    branchInfoQueue.Enqueue
                    (
                        new BranchInfo
                        (
                            x1,
                            y1,
                            branchInfo.Theta + deltaTheta,
                            branchInfo.Length * lengthScale,
                            branchInfo.Depth - 1
                        )
                    );

                    branchInfoQueue.Enqueue
                    (
                        new BranchInfo
                        (
                            x1,
                            y1,
                            branchInfo.Theta - deltaTheta,
                            branchInfo.Length * lengthScale,
                            branchInfo.Depth - 1
                        )
                    );
                }
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요