■ 극 좌표(Polar Coordinates)에서 곡선 그리기

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


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 이벤트를 설정한다.

 

            Load += Form_Load;

 

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

        {

            Bitmap bitmap = new Bitmap

            (

                ClientSize.Width,

                ClientSize.Height

            );

 

            using(Graphics graphics = Graphics.FromImage(bitmap))

            {

                graphics.Clear(Color.White);

 

                RectangleF rectangle = new RectangleF(-2.1f, -2.1f, 4.2f, 4.2f);

 

                PointF[] pointArray =

                {

                    new PointF(0, bitmap.Height),

                    new PointF(bitmap.Width, bitmap.Height),

                    new PointF(0, 0),

                };

 

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

                graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

                using(Pen pen = new Pen(Color.Blue, 0))

                {

                    pen.Color = Color.Blue;

 

                    graphics.DrawLine(pen, -2.1f, 0, 2.1f, 0);

 

                    const float LARGE_TICK = 0.1f;

                    const float SMALL_TICK = 0.05f;

 

                    for(float x = (int)-2.1f; x <= 2.1f; x += 1)

                    {

                        graphics.DrawLine(pen, x, -SMALL_TICK, x, SMALL_TICK);

                    }

 

                    for(float x = (int)-2.1f + 0.5f; x <= 2.1f; x += 1)

                    {

                        graphics.DrawLine(pen, x, -LARGE_TICK, x, LARGE_TICK);

                    }

 

                    graphics.DrawLine(pen, 0, -2.1f, 0, 2.1f);

 

                    for(float y = (int)-2.1f; y <= 2.1f; y += 1)

                    {

                        graphics.DrawLine(pen, -SMALL_TICK, y, SMALL_TICK, y);

                    }

 

                    for(float y = (int)-2.1f + 0.5f; y <= 2.1f; y += 1)

                    {

                        graphics.DrawLine(pen, -LARGE_TICK, y, LARGE_TICK, y);

                    }

 

                    DrawCurve(graphics);

                }

            }

 

            this.canvasPictureBox.Image = bitmap;

        }

 

        #endregion

 

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

 

        #region 곡선 그리기 - DrawCurve(graphics)

 

        /// <summary>

        /// 곡선 그리기

        /// </summary>

        /// <param name="graphics">그래픽스</param>

        private void DrawCurve(Graphics graphics)

        {

            const double DELTA  = Math.PI / 100.0;

            const double TWO_PI = 2 * Math.PI;

 

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

 

            double t = 0;

 

            while(t <= TWO_PI)

            {

                double r = 2 * Math.Sin(5 * t);

 

                float x = (float)(r * Math.Cos(t));

                float y = (float)(r * Math.Sin(t));

 

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

 

                t += DELTA;

            }

 

            using(Pen pen = new Pen(Color.Red, 0))

            {

                graphics.DrawPolygon(pen, pointList.ToArray());

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요