첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 반즐리 고사리 프랙탈(Barnsley Fern Fractal) 그리기

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


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 float[] probabilityArray = { 0.01f, 0.85f, 0.08f, 0.06f };

 

        /// <summary>

        /// 함수 배열

        /// </summary>

        private float[, ,] functionArray =

        {

            {

                { 0, 0     },

                { 0, 0.16f },

            },

            {

                {  0.85f, 0.04f },

                { -0.04f, 0.85f },

            },

            {

                { 0.2f , -0.26f },

                { 0.23f,  0.22f },

            },

            {

                { -0.15f, 0.28f },

                {  0.26f, 0.24f },

            }

        };

 

        /// <summary>

        /// 플러스 배열

        /// </summary>

        private float[,] plusArray =

        {

            {0, 0     },

            {0, 1.6f  },

            {0, 1.6f  },

            {0, 0.44f },

        };

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                         += Form_Load;

            this.canvasPictureBox.Resize += canvasPictureBox_Resize;

 

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

        {

            DrawBarnsleyFernFractal();

        }

 

        #endregion

        #region 캔버스 픽처 박스 크기 조정시 처리하기 - canvasPictureBox_Resize(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 크기 조정시 처리하기

        /// </summary>

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

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

        private void canvasPictureBox_Resize(object sender, EventArgs e)

        {

            DrawBarnsleyFernFractal();

        }

 

        #endregion

 

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

 

        #region 반즐리 고사리 프랙탈 그리기 - DrawBarnsleyFernFractal()

 

        /// <summary>

        /// 반즐리 고사리 프랙탈 그리기

        /// </summary>

        private void DrawBarnsleyFernFractal()

        {

            int imageWidth  = this.canvasPictureBox.ClientSize.Width;

            int imageHeight = this.canvasPictureBox.ClientSize.Height;

 

            Bitmap bitmap = new Bitmap(imageWidth, imageHeight);

 

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

            {

                graphics.Clear(Color.Black);

 

                Random random = new Random();

 

                int   functionValue = 0;

                int   xIndex;

                int   yIndex;

                float x = 1;

                float y = 1;

                float x1;

                float y1;

 

                for(int i = 1; i <= 100000; i++)

                {

                    double randomValue = random.NextDouble();

 

                    for(int j = 0; j <= 3; j++)

                    {

                        randomValue = randomValue - probabilityArray[j];

 

                        if(randomValue <= 0)

                        {

                            functionValue = j;

 

                            break;

                        }

                    }

 

                    x1 = x * functionArray[functionValue, 0, 0] + y * functionArray[functionValue, 0, 1] + plusArray[functionValue, 0];

                    y1 = x * functionArray[functionValue, 1, 0] + y * functionArray[functionValue, 1, 1] + plusArray[functionValue, 1];

 

                    x = x1;

                    y = y1;

 

                    const float xMinimum = -4;

                    const float xMaximum = 4;

                    const float yMinimum = -0.1f;

                    const float yMaximum = 10.1f;

                    const float width    = xMaximum - xMinimum;

                    const float height   = yMaximum - yMinimum;

 

                    xIndex = (int)Math.Round((x - xMinimum) / width * this.canvasPictureBox.ClientSize.Width);

                    yIndex = (int)Math.Round((this.canvasPictureBox.ClientSize.Height - 1) - (y - yMinimum) / height * imageHeight);

 

                    if((xIndex >= 0) && (yIndex >= 0) && (xIndex < imageWidth) && (yIndex < imageHeight))

                    {

                        bitmap.SetPixel(xIndex, yIndex, Color.Lime);

                    }

                }

            }

 

            this.canvasPictureBox.BackgroundImage = bitmap;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요