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

728x90
반응형

■ 시어핀스키 가스켓(Sierpinski Gasket) 그리기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 코너 포인트 리스트

        /// </summary>

        private List<PointF> cornerPointList;

 

        /// <summary>

        /// 마지막 포인트

        /// </summary>

        private PointF lastPoint;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load            += Form_Load;

            Resize          += Form_Resize;

            this.timer.Tick += timer_Tick;

 

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

        {

            DefineCorner();

        }

 

        #endregion

        #region 폼 크기 조정시 처리하기 - Form_Resize(sender, e)

 

        /// <summary>

        /// 폼 크기 조정시 처리하기

        /// </summary>

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

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

        private void Form_Resize(object sender, EventArgs e)

        {

            DefineCorner();

 

            using(Graphics graphics = CreateGraphics())

            {

                graphics.Clear(BackColor);

            }

        }

 

        #endregion

        #region 타이머 틱 처리하기 - timer_Tick(sender, e)

 

        /// <summary>

        /// 타이머 틱 처리하기

        /// </summary>

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

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

        private void timer_Tick(object sender, EventArgs e)

        {

            Random random = new Random();

 

            using(Graphics graphics = CreateGraphics())

            {

                foreach(PointF cornerPoint in this.cornerPointList)

                {

                    graphics.FillEllipse(Brushes.White, cornerPoint.X - 2, cornerPoint.Y - 2, 4, 4);

 

                    graphics.DrawEllipse(Pens.Blue, cornerPoint.X - 2, cornerPoint.Y - 2, 4, 4);

                }

 

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

                {

                    int j = random.Next(0, 3);

 

                    this.lastPoint = new PointF

                    (

                        (this.lastPoint.X + this.cornerPointList[j].X) / 2,

                        (this.lastPoint.Y + this.cornerPointList[j].Y) / 2

                    );

 

                    graphics.DrawLine

                    (

                        Pens.Red,

                        this.lastPoint.X,

                        this.lastPoint.Y,

                        this.lastPoint.X + 1,

                        this.lastPoint.Y + 1

                    );

                }

            }

        }

 

        #endregion

 

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

 

        #region 코너 정의하기 - DefineCorner()

 

        /// <summary>

        /// 코너 정의하기

        /// </summary>

        private void DefineCorner()

        {

            this.cornerPointList = new List<PointF>();

 

            this.cornerPointList.Add(new PointF(ClientSize.Width / 2, 10));

            this.cornerPointList.Add(new PointF(10, ClientSize.Height - 10));

            this.cornerPointList.Add(new PointF(ClientSize.Width - 10, ClientSize.Height - 10));

 

            this.lastPoint = this.cornerPointList[0];

        }

 

        #endregion

    }

}

 

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

728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요