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

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)
        {
            float a = float.Parse(this.aTextBox.Text);
            int   n = int.Parse(this.nTextBox.Text);
            int   d = int.Parse(this.dTextBox.Text);

            Bitmap bitmap = new Bitmap
            (
                this.canvasPictureBox.ClientSize.Width,
                this.canvasPictureBox.ClientSize.Height
            );

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.White);

                graphics.SmoothingMode = SmoothingMode.AntiAlias;

                RectangleF rectangle = new RectangleF
                (
                    -a - 0.1f,
                    -a - 0.1f,
                    2 * a + 0.2f,
                    2 * a + 0.2f
                );

                PointF[] pointArray =
                {
                    new PointF(0, bitmap.Height),
                    new PointF(bitmap.Width, bitmap.Height),
                    new PointF(0, 0),
                };

                graphics.Transform = new Matrix(rectangle, pointArray);

                DrawCurve(graphics, a, n, d);

                DrawAxes(graphics, rectangle.Right);

                this.canvasPictureBox.Image = bitmap;
            }
        }

        #endregion

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

        #region 곡선 그리기 - DrawCurve(graphics, a, n, d)

        /// <summary>
        /// 곡선 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="a">A</param>
        /// <param name="n">N</param>
        /// <param name="d">D</param>
        private void DrawCurve(Graphics graphics, float a, int n, int d)
        {
            const int pointCount = 1000;

            double period = Math.PI * d;

            if((n % 2 == 0) || (d % 2 == 0))
            {
                period *= 2;
            }

            double dtheta = period / pointCount;

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

            double k = (double)n / d;

            for(int i = 0; i < pointCount; i++)
            {
                double theta = i * dtheta;
                double r     = a * Math.Cos(k * theta);
                float  x     = (float)(r * Math.Cos(theta));
                float  y     = (float)(r * Math.Sin(theta));

                pointList.Add(new PointF(x, y));
            }

            graphics.FillPolygon(Brushes.LightBlue, pointList.ToArray());

            using(Pen pen = new Pen(Color.Red, 0))
            {
                graphics.DrawLines(pen, pointList.ToArray());
            }
        }

        #endregion
        #region 축 그리기 - DrawAxes(graphics, worldMaximumX)

        /// <summary>
        /// 축 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="worldMaximumX">월드 최대 X</param>
        private void DrawAxes(Graphics graphics, float worldMaximumX)
        {
            int maximumX = (int)worldMaximumX;

            using(Pen pen = new Pen(Color.Black, 0))
            {
                graphics.DrawLine(pen, -worldMaximumX, 0             , worldMaximumX, 0            );
                graphics.DrawLine(pen, 0             , -worldMaximumX, 0            , worldMaximumX);

                float tic = 0.1f;

                for(int x = -maximumX; x <= maximumX; x++)
                {
                    graphics.DrawLine(pen,  x  , -tic, x  , tic);
                    graphics.DrawLine(pen, -tic,  x  , tic, x  );
                }
            }
        }

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

댓글을 달아 주세요