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

■ 사인(Sine)과 코사인(Cosine)을 사용해 원과 타원 그리기

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


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

 

            Load  += Form_Load;

            Paint += Form_Paint;

        }

 

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

        {

            ResizeRedraw = true;

        }

 

        #endregion

        #region 폼 페인트시 처리하기 - Form_Paint(sender, e)

 

        /// <summary>

        /// 폼 페인트시 처리하기

        /// </summary>

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

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

        private void Form_Paint(object sender, PaintEventArgs e)

        {

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

            float centerX = ClientRectangle.Width  / 2f;

            float centerY = ClientRectangle.Height / 2f;

 

            float radiusX = Math.Min(centerX, centerY) - 10;

            float radiusY = radiusX;

 

            DrawCircle

            (

                e.Graphics,

                Pens.Blue,

                Pens.Orange,

                centerX,

                centerY,

                radiusX,

                radiusY,

                100,

                12,

                0.1f

            );

 

            radiusX *= 0.8f;

            radiusY *= 0.6f;

 

            DrawCircle

            (

                e.Graphics,

                Pens.Red,

                Pens.Black,

                centerX,

                centerY,

                radiusX,

                radiusY,

                16,

                16,

                0.1f

            );

        }

 

        #endregion

 

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

 

        #region 원 그리기 - DrawCircle(graphics, circlePen, tickPen, centerX, centerY, radiusX, radiusY, thetaCount, tickCount, tickFraction)

 

        /// <summary>

        /// 원 그리기

        /// </summary>

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

        /// <param name="circlePen">원 펜</param>

        /// <param name="tickPen">틱 펜</param>

        /// <param name="centerX">중심 X</param>

        /// <param name="centerY">중심 Y</param>

        /// <param name="radiusX">반경 X</param>

        /// <param name="radiusY">반경 Y</param>

        /// <param name="thetaCount">세타 카운트</param>

        /// <param name="tickCount">틱 카운트</param>

        /// <param name="tickFraction">틱 조각</param>

        private void DrawCircle

        (

            Graphics graphics,

            Pen      circlePen,

            Pen      tickPen,

            float    centerX,

            float    centerY,

            float    radiusX,

            float    radiusY,

            float    thetaCount,

            float    tickCount,

            float    tickFraction

        )

        {

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

 

            float deltaTheta = (float)(2 * Math.PI / thetaCount);

 

            float theta = 0;

 

            for(int i = 0; i < thetaCount; i++)

            {

                float x = (float)(centerX + radiusX * Math.Cos(theta));

                float y = (float)(centerY + radiusY * Math.Sin(theta));

 

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

 

                theta += deltaTheta;

            }

 

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

 

            deltaTheta = (float)(2 * Math.PI / tickCount);

 

            theta = 0;

 

            float temporaryX = radiusX * (1 - tickFraction);

            float temporaryT = radiusY * (1 - tickFraction);

 

            for(int i = 0; i < tickCount; i++)

            {

                float x1 = (float)(centerX + radiusX * Math.Cos(theta));

                float y1 = (float)(centerY + radiusY * Math.Sin(theta));

 

                float x2 = (float)(centerX + temporaryX * Math.Cos(theta));

                float y2 = (float)(centerY + temporaryT * Math.Sin(theta));

 

                graphics.DrawLine(tickPen, x1, y1, x2, y2);

 

                theta += deltaTheta;

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요