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

728x90
반응형

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Collections.Generic;
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.drawButton.Click += drawButton_Click;

            #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)
        {
            int a              = int.Parse(this.aTextBox.Text);
            int b              = int.Parse(this.bTextBox.Text);
            int c              = int.Parse(this.cTextBox.Text);
            int iterationCount = int.Parse(iterationCountTextBox.Text);

            int width  = this.canvasPictureBox.ClientSize.Width;
            int height = this.canvasPictureBox.ClientSize.Height;

            Bitmap bitmap = new Bitmap(width, height);

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;

                int halfWidth  = width  / 2;
                int halfHeight = height / 2;

                double t        = 0;
                double deltaT   = Math.PI / iterationCount;
                double maximumT = 2 * Math.PI * b / GetGreatestCommonDivisor(a, b);
                double x1       = halfWidth  + GetX(t, a, b, c);
                double y1       = halfHeight + GetY(t, a, b, c);

                List<PointF> pointList = new List<PointF>();

                pointList.Add(new PointF((float)x1, (float)y1));

                while(t <= maximumT)
                {
                    t += deltaT;

                    x1 = halfWidth  + GetX(t, a, b, c);
                    y1 = halfHeight + GetY(t, a, b, c);

                    pointList.Add(new PointF((float)x1, (float)y1));
                }

                graphics.DrawPolygon(Pens.Red, pointList.ToArray());
            }

            this.canvasPictureBox.Image = bitmap;
        }

        #endregion

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

        #region 최대 공약수 구하기 - GetGreatestCommonDivisor(a, b)

        /// <summary>
        /// 최대 공약수 구하기
        /// </summary>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <returns>최대 공약수</returns>
        private long GetGreatestCommonDivisor(long a, long b)
        {
            a = Math.Abs(a);
            b = Math.Abs(b);

            if(a < b)
            {
                long temporary = a;

                a = b;
                b = temporary;
            }

            for(;;)
            {
                long remainder = a % b;

                if(remainder == 0)
                {
                    return b;
                }

                a = b;
                b = remainder;
            };
        }

        #endregion
        #region 최소 공배수 구하기 - GetLeastCommonMultiple(a, b)

        /// <summary>
        /// 최소 공배수 구하기
        /// </summary>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <returns>최소 공배수</returns>
        private long GetLeastCommonMultiple(long a, long b)
        {
            return a * b / GetGreatestCommonDivisor(a, b);
        }

        #endregion
        #region X 구하기 - GetX(t, a, b, c)

        /// <summary>
        /// X 구하기
        /// </summary>
        /// <param name="t">T</param>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <param name="c">C</param>
        /// <returns>X</returns>
        private double GetX(double t, double a, double b, double c)
        {
            return (a - b) * Math.Cos(t) + c * Math.Cos((a - b) / b * t);
        }

        #endregion
        #region Y 구하기 - GetX(t, a, b, c)

        /// <summary>
        /// Y 구하기
        /// </summary>
        /// <param name="t">T</param>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <param name="c">C</param>
        /// <returns>Y</returns>
        private double GetY(double t, double a, double b, double c)
        {
            return (a - b) * Math.Sin(t) - c * Math.Sin((a - b) / b * t);
        }

        #endregion
    }
}
728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요