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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

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

        #region Field

        /// <summary>
        /// 갱신 여부
        /// </summary>
        private bool refresh;

        /// <summary>
        /// 마지막 X
        /// </summary>
        private float lastX;
        
        /// <summary>
        /// 마지막 Y
        /// </summary>
        private float lastY;

        /// <summary>
        /// 비트맵
        /// </summary>
        private Bitmap bitmap;

        /// <summary>
        /// 난수 발생기
        /// </summary>
        private Random random;

        #endregion

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

        #region 생성자 - MainForm()

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

            this.random = new Random(DateTime.Now.Millisecond);
        }

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

            float totalLength;
            float startX;
            float startY;
            float startLength;

            if(this.canvasPictureBox.ClientSize.Height < this.canvasPictureBox.ClientSize.Width)
            {
                totalLength = (float)(0.9 * this.canvasPictureBox.ClientSize.Height);
            }
            else
            {
                totalLength = (float)(0.9 * this.canvasPictureBox.ClientSize.Width);
            }

            startX = (this.canvasPictureBox.ClientSize.Width  - totalLength) / 2;
            startY = (this.canvasPictureBox.ClientSize.Height - totalLength) / 2;

            startLength = (float)(totalLength / (Math.Pow(2, depth) - 1));

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

            this.canvasPictureBox.Image = this.bitmap;

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.SmoothingMode      = SmoothingMode.HighQuality;
                graphics.TextRenderingHint  = TextRenderingHint.ClearTypeGridFit;
                graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;

                graphics.Clear(Color.White);

                this.lastX = (int)startX;
                this.lastY = (int)startY;

                DrawHilbertCurve(graphics, depth, startLength, 0);
            }

            this.canvasPictureBox.Refresh();

            Cursor = Cursors.Default;
        }

        #endregion

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

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

        /// <summary>
        /// 선 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="deltaX">델타 X</param>
        /// <param name="deltaY">델타 Y</param>
        private void DrawLine(Graphics graphics, float deltaX, float deltaY)
        {
            Pen pen = new Pen(Color.FromArgb(this.random.Next(255), this.random.Next(255), this.random.Next(255)), 2);

            graphics.DrawLine(pen, this.lastX, this.lastY, this.lastX + deltaX, this.lastY + deltaY);

            this.lastX = this.lastX + deltaX;
            this.lastY = this.lastY + deltaY;
        }

        #endregion
        #region 힐버트 곡선 그리기 - DrawHilbertCurve(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 DrawHilbertCurve(Graphics graphics, int depth, float deltaX, float deltaY)
        {
            if(depth > 1)
            {
                DrawHilbertCurve(graphics, depth - 1, deltaY, deltaX);
            }

            DrawLine(graphics, deltaX, deltaY);

            if(depth > 1)
            {
                DrawHilbertCurve(graphics, depth - 1, deltaX, deltaY);
            }

            DrawLine(graphics, deltaY, deltaX);

            if(depth > 1)
            {
                DrawHilbertCurve(graphics, depth - 1, deltaX, deltaY);
            }

            DrawLine(graphics, -deltaX, -deltaY);

            if(depth > 1)
            {
                DrawHilbertCurve(graphics, depth - 1, -deltaY, -deltaX);
            }

            if(this.refresh)
            {
                this.canvasPictureBox.Refresh();
            }
        }

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

댓글을 달아 주세요