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

■ 마우스 아래 파이 슬라이스 구하기

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


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

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 타원 사각형

        /// </summary>

        private Rectangle ellipseRectangle;

 

        /// <summary>

        /// 중심 포인트

        /// </summary>

        private Point centerPoint;

 

        /// <summary>

        /// X축 반지름

        /// </summary>

        private float radiusX;

        

        /// <summary>

        /// Y축 반지름

        /// </summary>

        private float radiusY;

 

        /// <summary>

        /// 각도 배열

        /// </summary>

        private float[] angleArray = { 0, 45, 80, 110, 130, 170, 220, 245, 300, 360 };

 

        /// <summary>

        /// 브러시 배열

        /// </summary>

        private Brush[] brushArray =

        {

            Brushes.Red,

            Brushes.LightGreen,

            Brushes.LightBlue,

            Brushes.Yellow,

            Brushes.Orange,

            Brushes.White,

            Brushes.Cyan,

            Brushes.Pink,

            Brushes.Black,

        };

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                            += Form_Load;

            this.canvasPictureBox.Paint     += canvasPictureBox_Paint;

            this.canvasPictureBox.MouseMove += canvasPictureBox_MouseMove;

 

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

        {

            const int MARGIN = 10;

 

            int width  = this.canvasPictureBox.ClientSize.Width  - 2 * MARGIN;

            int height = this.canvasPictureBox.ClientSize.Height - 2 * MARGIN;

 

            this.ellipseRectangle = new Rectangle(MARGIN, MARGIN, width, height);

 

            this.centerPoint = new Point

            (

                this.canvasPictureBox.ClientSize.Width  / 2,

                this.canvasPictureBox.ClientSize.Height / 2

            );

 

            this.radiusX = width  / 2;

            this.radiusY = height / 2;

        }

 

        #endregion

        #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void canvasPictureBox_Paint(object sender, PaintEventArgs e)

        {

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

            e.Graphics.FillEllipse(Brushes.Blue, this.ellipseRectangle);

 

            for(int i = 1; i < this.angleArray.Length; i++)

            {

                e.Graphics.FillPie

                (

                    this.brushArray[i - 1],

                    this.ellipseRectangle,

                    this.angleArray[i],

                    this.angleArray[i - 1] - this.angleArray[i]

                );

            }

 

            e.Graphics.DrawEllipse(Pens.Blue, this.ellipseRectangle);

        }

 

        #endregion

        #region 캔버스 픽처 박스 마우스 이동시 처리하기 - canvasPictureBox_MouseMove(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 마우스 이동시 처리하기

        /// </summary>

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

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

        private void canvasPictureBox_MouseMove(object sender, MouseEventArgs e)

        {

            int sliceNumber = GetSliceNumber(this.ellipseRectangle, this.angleArray, e.Location);

 

            if(sliceNumber == -1)

            {

                this.sliceNumberLabel.Text = string.Empty;

            }

            else

            {

                this.sliceNumberLabel.Text = sliceNumber.ToString();

            }

        }

 

        #endregion

 

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

 

        #region 슬라이스 번호 구하기 - GetSliceNumber(ellipseRectangle, angleArray, point)

 

        /// <summary>

        /// 슬라이스 번호 구하기

        /// </summary>

        /// <param name="ellipseRectangle">타원 사각형</param>

        /// <param name="angleArray">각도 배열</param>

        /// <param name="point">포인트</param>

        /// <returns>슬라이스 번호</returns>

        private int GetSliceNumber(Rectangle ellipseRectangle, float[] angleArray, Point point)

        {

            float radiusX = ellipseRectangle.Width  / 2;

            float radiusY = ellipseRectangle.Height / 2;

            float centerX = ellipseRectangle.X + radiusX;

            float centerY = ellipseRectangle.Y + radiusY;

            float deltaX  = point.X - centerX;

            float deltaY  = point.Y - centerY;

            float value   = deltaX * deltaX / radiusX / radiusX + deltaY * deltaY / radiusY / radiusY;

 

            if(value < 0.0001)

            {

                return -1;

            }

 

            if(value > 1)

            {

                return -1;

            }

 

            double angle = Math.Atan2(deltaY, deltaX);

 

            if(angle < 0)

            {

                angle += 2 * Math.PI;

            }

 

            angle = angle * 180 / Math.PI;

 

            for(int i = 0; i < this.angleArray.Length - 1; i++)

            {

                if(angle <= this.angleArray[i + 1])

                {

                    return i;

                }

            }

 

            throw new Exception("각도를 찾을 수 없습니다 : " + angle);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요