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

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();

            ResizeRedraw = true;

            #region 이벤트를 설정한다.

            this.drawButton.Click       += drawButton_Click;
            this.canvasPictureBox.Paint += canvasPictureBox_Paint;

            #endregion
        }

        #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)
        {
            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)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            e.Graphics.Clear(this.canvasPictureBox.BackColor);

            float scale = Math.Min
            (
                this.canvasPictureBox.ClientSize.Width  * 0.45f,
                this.canvasPictureBox.ClientSize.Height * 0.45f
            );

            e.Graphics.ScaleTransform(scale, scale);

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

            float a  = float.Parse(this.aTextBox.Text );
            float b  = float.Parse(this.bTextBox.Text );
            float h  = float.Parse(this.hTextBox.Text );
            float dt = float.Parse(this.dtTextBox.Text);

            DrawEpitrochoid(e.Graphics, a, b, h, dt);
        }

        #endregion

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

        #region X 좌표 구하기 - GetX(a, b, h, t)

        /// <summary>
        /// X 좌표 구하기
        /// </summary>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <param name="h">H</param>
        /// <param name="t">T</param>
        /// <returns>X 좌표</returns>
        private float GetX(float a, float b, float h, float t)
        {
            float value = (float)((a + b) * Math.Cos(t) - h * Math.Cos(t * (a + b) / b));

            return value / (a + b + h);
        }

        #endregion
        #region Y 좌표 구하기 - GetY(a, b, h, t)

        /// <summary>
        /// Y 좌표 구하기
        /// </summary>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <param name="h">H</param>
        /// <param name="t">T</param>
        /// <returns></returns>
        private float GetY(float a, float b, float h, float t)
        {
            float value = (float)((a + b) * Math.Sin(t) - h * Math.Sin(t * (a + b) / b));

            return value / (a + b + h);
        }

        #endregion
        #region 에피트로코이드 그리기 - DrawEpitrochoid(graphics, a, b, h, dt)

        /// <summary>
        /// 에피트로코이드 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <param name="h">H</param>
        /// <param name="dt">DT</param>
        private void DrawEpitrochoid(Graphics graphics, float a, float b, float h, float dt)
        {
            float stopT = (float)(b * 2 * Math.PI);

            using(Pen pen = new Pen(Color.White, 0))
            {
                PointF pt0;
                PointF pt1;

                pt0 = new PointF(GetX(a, b, h, 0), GetY(a, b, h, 0));

                for(float t = dt; t <= stopT; t += dt)
                {
                    pt1 = new PointF(GetX(a, b, h, t), GetY(a, b, h, t));

                    graphics.DrawLine(pen, pt0, pt1);

                    pt0 = pt1;
                }
            }
        }

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

댓글을 달아 주세요