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

■ 피코버 팝콘 프랙탈(Pickover Popcorn 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>

        /// 세계 최소 X

        /// </summary>

        private const float WORLD_MINIMUM_X = -4.0f;

 

        /// <summary>

        /// 세계 최대 X

        /// </summary>

        private const float WORLD_MAXIMUM_X = 4.0f;

 

        /// <summary>

        /// 세계 최소 Y

        /// </summary>

        private const float WORLD_MINIMUM_Y = -3.0f;

 

        /// <summary>

        /// 세계 최대 Y

        /// </summary>

        private const float WORLD_MAXIMUM_Y = 3.0f;

 

        /// <summary>

        /// 세계 너비

        /// </summary>

        private const float WORLD_WIDTH = (WORLD_MAXIMUM_X - WORLD_MINIMUM_X);

 

        /// <summary>

        /// 세계 높이

        /// </summary>

        private const float WORLD_HEIGHT = (WORLD_MAXIMUM_Y - WORLD_MINIMUM_Y);

 

        /// <summary>

        /// 델타 최소 X

        /// </summary>

        private const float DELTA_MINIMUM_X = 0f;

 

        /// <summary>

        /// 델타 최대 X

        /// </summary>

        private const float DELTA_MAXIMUM_X = 400f;

 

        /// <summary>

        /// 델타 최소 Y

        /// </summary>

        private const float DELTA_MINIMUM_Y = 0f;

 

        /// <summary>

        /// 델타 최대 Y

        /// </summary>

        private const float DELTA_MAXIMUM_Y = 300f;

 

        /// <summary>

        /// 델타 너비

        /// </summary>

        private const float DELTA_WIDTH = (DELTA_MAXIMUM_X - DELTA_MINIMUM_X);

 

        /// <summary>

        /// 델타 높이

        /// </summary>

        private const float DELTA_HEIGHT = (DELTA_MAXIMUM_Y - DELTA_MINIMUM_Y);

 

        /// <summary>

        /// RGB 빨간색

        /// </summary>

        private const int RGB_RED = 0;

 

        /// <summary>

        /// RGB 녹색

        /// </summary>

        private const int RGB_GREEN = 1;

 

        /// <summary>

        /// RGB 파란색

        /// </summary>

        private const int REB_BLUE = 2;

 

        /// <summary>

        /// RGB 타입

        /// </summary>

        private int rgbType = RGB_RED;

 

        /// <summary>

        /// 비트맵

        /// </summary>

        private Bitmap bitmap;

 

        /// <summary>

        /// H

        /// </summary>

        private float h;

 

        /// <summary>

        /// 반복 카운트

        /// </summary>

        private int iterationCount;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                             += Form_Load;

            this.drawButton.Click            += drawButton_Click;

            this.clearButton.Click           += clearButton_Click;

            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)

        {

            this.bitmap = new Bitmap

            (

                this.canvasPictureBox.ClientSize.Width,

                this.canvasPictureBox.ClientSize.Height

            );

 

            this.canvasPictureBox.Image = this.bitmap;

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void drawButton_Click(object sender, EventArgs e)

        {

            SetParameter();

 

            this.bitmap = new Bitmap

            (

                this.canvasPictureBox.ClientSize.Width,

                this.canvasPictureBox.ClientSize.Height

            );

 

            this.canvasPictureBox.Image = this.bitmap;

 

            int dx = int.Parse(this.dxTextBox.Text);

 

            this.rgbType = RGB_RED;

 

            for(int x = 0; x < this.bitmap.Width; x += dx)

            {

                for(int y = 0; y < this.bitmap.Height; y += dx)

                {

                    Draw(this.bitmap, this.h, x, y, this.iterationCount, this.rgbType);

 

                    this.rgbType = ++this.rgbType % 3;

                }

 

                this.canvasPictureBox.Refresh();

            }

 

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

        #region 지우기 버튼 클릭시 처리하기 - clearButton_Click(sender, e)

 

        /// <summary>

        /// 지우기 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void clearButton_Click(object sender, EventArgs e)

        {

            this.bitmap = new Bitmap

            (

                this.canvasPictureBox.ClientSize.Width,

                this.canvasPictureBox.ClientSize.Height

            );

 

            this.canvasPictureBox.Image = this.bitmap;

 

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void canvasPictureBox_MouseClick(object sender, MouseEventArgs e)

        {

            SetParameter();

 

            Draw(this.bitmap, this.h, e.X, e.Y, this.iterationCount, this.rgbType);

 

            this.rgbType = ++this.rgbType % 3;

 

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

 

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

 

        #region 매개 변수 설정하기 - SetParameter()

 

        /// <summary>

        /// 매개 변수 설정하기

        /// </summary>

        private void SetParameter()

        {

            this.iterationCount = int.Parse(this.iterationCountTextBox.Text);

 

            this.h = float.Parse(this.hTextBox.Text);

        }

 

        #endregion

        #region 세계 좌표를 장치 좌표로 변환하기 - WorldToDevice(worldX, worldY, deviceX, deviceY)

 

        /// <summary>

        /// 세계 좌표를 장치 좌표로 변환하기

        /// </summary>

        /// <param name="worldX">세계 X 좌표</param>

        /// <param name="worldY">세계 Y 좌표</param>

        /// <param name="deviceX">장치 X 좌표</param>

        /// <param name="deviceY">장치 Y 좌표</param>

        private void WorldToDevice(float worldX, float worldY, out int deviceX, out int deviceY)

        {

            deviceX = (int)(DELTA_MINIMUM_X + DELTA_WIDTH  * (worldX - WORLD_MINIMUM_X) / WORLD_WIDTH );

            deviceY = (int)(DELTA_MINIMUM_Y + DELTA_HEIGHT * (worldY - WORLD_MINIMUM_Y) / WORLD_HEIGHT);

        }

 

        #endregion

        #region 장치 좌표를 세계 좌표로 변환하기 - DeviceToWorld(deviceX, deviceY, worldX, worldY)

 

        /// <summary>

        /// 장치 좌표를 세계 좌표로 변환하기

        /// </summary>

        /// <param name="deviceX">장치 X 좌표</param>

        /// <param name="deviceY">장치 Y 좌표</param>

        /// <param name="worldX">세계 X 좌표</param>

        /// <param name="worldY">세계 Y 좌표</param>

        private void DeviceToWorld(int deviceX, int deviceY, out float worldX, out float worldY)

        {

            worldX = WORLD_MINIMUM_X + WORLD_WIDTH  * (deviceX - DELTA_MINIMUM_X) / DELTA_WIDTH;

            worldY = WORLD_MINIMUM_Y + WORLD_HEIGHT * (deviceY - DELTA_MINIMUM_Y) / DELTA_HEIGHT;

        }

 

        #endregion

        #region 그리기 - Draw(bitmap, h, x, y, iterationCount, rgbType)

 

        /// <summary>

        /// 그리기

        /// </summary>

        /// <param name="bitmap">비트맵</param>

        /// <param name="h">H</param>

        /// <param name="x">X 좌표</param>

        /// <param name="y">Y 좌표</param>

        /// <param name="iterationCount">반복 카운트</param>

        /// <param name="rgbType">RGB 타입</param>

        private void Draw(Bitmap bitmap, float h, int x, int y, int iterationCount, int rgbType)

        {

            float worldX;

            float worldY;

 

            DeviceToWorld(x, y, out worldX, out worldY);

 

            bitmap.SetPixel(x, y, Color.Blue);

 

            for(int i = 0; i < iterationCount; i++)

            {

                float newX = (float)(worldX - h * Math.Sin(worldY + Math.Tan(3 * worldY)));

                float newY = (float)(worldY - h * Math.Sin(worldX + Math.Tan(3 * worldX)));

 

                worldX = newX;

                worldY = newY;

 

                WorldToDevice(worldX, worldY, out x, out y);

 

                if(x >= 0 && x < bitmap.Width && y >= 0 && y < bitmap.Height)

                {

                    Color color = bitmap.GetPixel(x, y);

 

                    int newR = color.R;

                    int newG = color.G;

                    int newB = color.B;

 

                    switch(this.rgbType)

                    {

                        case RGB_RED   : newR = newR + (255 - newR) / 2; break;

                        case RGB_GREEN : newG = newG + (255 - newG) / 2; break;

                        case REB_BLUE  : newB = newB + (255 - newB) / 2; break;

                    }

 

                    color = Color.FromArgb(255, newR, newG, newB);

 

                    bitmap.SetPixel(x, y, color);

                }

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요