■ 비교차 스타 그리기

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


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

 

            Paint += Form_Paint;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

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

 

            int width  = (int)(ClientRectangle.Width  * 0.9);

            int height = (int)(ClientRectangle.Height * 0.9);

 

            int size = (width > height) ? height : width;

 

            Rectangle rectangle = new Rectangle((int)((ClientRectangle.Width - size) / 2d), (int)((ClientRectangle.Height - size) / 2d), size, size);

 

            PointF[] pointArray = GetStarPointArray(5, rectangle);

 

            e.Graphics.DrawPolygon(Pens.Blue, pointArray);

 

            e.Graphics.DrawRectangle(Pens.Red, rectangle);

        }

 

        #endregion

 

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

 

        #region 스타 포인트 배열 구하기 - GetStarPointArray(vertexCount, boundRectangle)

 

        /// <summary>

        /// 스타 포인트 배열 구하기

        /// </summary>

        /// <param name="vertexCount">꼭지점 카운트</param>

        /// <param name="boundRectangle">테두리 사각형</param>

        /// <returns>스타 포인트 배열</returns>

        private PointF[] GetStarPointArray(int vertexCount, Rectangle boundRectangle)

        {

            PointF[] pointArray = new PointF[2 * vertexCount];

 

            double rx1 = boundRectangle.Width / 2;

            double ry1 = boundRectangle.Height / 2;

            double rx2 = rx1 * 0.5;

            double ry2 = ry1 * 0.5;

            double cx  = boundRectangle.X + rx1;

            double cy  = boundRectangle.Y + ry1;

 

            double theta  = -Math.PI / 2;

            double dtheta = Math.PI / vertexCount;

 

            for(int i = 0; i < 2 * vertexCount; i += 2)

            {

                pointArray[i] = new PointF

                (

                    (float)(cx + rx1 * Math.Cos(theta)),

                    (float)(cy + ry1 * Math.Sin(theta))

                );

 

                theta += dtheta;

 

                pointArray[i + 1] = new PointF

                (

                    (float)(cx + rx2 * Math.Cos(theta)),

                    (float)(cy + ry2 * Math.Sin(theta))

                );

 

                theta += dtheta;

            }

 

            return pointArray;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요