첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ 소용돌이 모양 프랙탈(Curlicue Fractal)을 그리는 방법을 보여준다.

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
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

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

            #region 이벤트를 설정한다.

            this.runButton.Click                         += runButton_Click;
            this.sValueTypeComboBox.SelectedIndexChanged += sValueTypeComboBox_SelectedIndexChanged;
            this.canvasPictureBox.Paint                  += canvasPictureBox_Paint;
            this.canvasPictureBox.Resize                 += canvasPictureBox_Resize;

            #endregion
        }

        #endregion

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

        #region 실행 버튼 클릭시 처리하기 - runButton_Click(sender, e)

        /// <summary>
        /// 실행 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void runButton_Click(object sender, EventArgs e)
        {
            this.canvasPictureBox.Invalidate();
        }

        #endregion
        #region S 값 타입 콤보 박스 선택 인덱스 변경시 처리하기 - sValueTypeComboBox_SelectedIndexChanged(sender, e)

        /// <summary>
        /// S 값 타입 콤보 박스 선택 인덱스 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void sValueTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(this.sValueTypeComboBox.Text.ToLower())
            {
                case "pi"      : this.sValueTextBox.Text = Math.PI.ToString();      break;
                case "ln(2)"   : this.sValueTextBox.Text = Math.Log(2).ToString();  break;
                case "e"       : this.sValueTextBox.Text = Math.E.ToString();       break;
                case "sqrt(2)" : this.sValueTextBox.Text = Math.Sqrt(2).ToString(); break;
                case "sqrt(3)" : this.sValueTextBox.Text = Math.Sqrt(3).ToString(); break;
                case "sqrt(5)" : this.sValueTextBox.Text = Math.Sqrt(5).ToString(); break;
                case "lambda"  :

                    this.sValueTextBox.Text = "0.577215664901532860606512090082402431042";

                    break;

                case "golden ratio" :

                    this.sValueTextBox.Text = "1.618033988749894848204586834365638117720";

                    break;

                case "feigenbaum" :

                    this.sValueTextBox.Text = "4.6692016091029906718532038204662016172581855774757686327456513430041343302113147371386897440239480138";

                    break;
            }

            this.canvasPictureBox.Invalidate();
        }

        #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.Invalidate();
        }

        #endregion
        #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e)

        /// <summary>
        /// 캔버스 픽처 박스 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void canvasPictureBox_Paint(object sender, PaintEventArgs e)
        {
            DrawCurlicueFractal(e.Graphics);
        }

        #endregion

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

        #region 소용돌이 모양 프랙탈 그리기 - DrawCurlicueFractal(graphics)

        /// <summary>
        /// 소용돌이 모양 프랙탈 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        private void DrawCurlicueFractal(Graphics graphics)
        {
            const int scale = 3;

            graphics.ScaleTransform
            (
                scale,
                scale,
                MatrixOrder.Append
            );

            graphics.TranslateTransform
            (
                this.canvasPictureBox.ClientSize.Width  / 2,
                this.canvasPictureBox.ClientSize.Height / 2,
                MatrixOrder.Append
            );

            double sValue = double.Parse(sValueTextBox.Text);
            double theta;
            double phi;
            double x0;
            double y0;
            double x1;
            double y1;

            theta = 0;
            phi   = 0;
            x0    = 0;
            y0    = 0;

            using(Pen pen = new Pen(Color.Red, 0))
            {
                for(int i = 1; i <= 10000; i++)
                {
                    x1 = x0 + Math.Cos(phi);
                    y1 = y0 + Math.Sin(phi);

                    graphics.DrawLine(pen, (float)x0, (float)-y0, (float)x1, (float)-y1);

                    x0 = x1;
                    y0 = y1;

                    phi   = (theta + phi) % (2 * Math.PI);
                    theta = (theta + 2 * Math.PI * sValue) % (2 * Math.PI);
                }
            }
        }

        #endregion
    }
}
728x90
그리드형(광고전용)
Posted by icodebroker
,