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

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 원 중심 X 좌표
        /// </summary>
        private int circleCenterX;

        /// <summary>
        /// 원 중심 Y 좌표
        /// </summary>
        private int circleCenterY;

        /// <summary>
        /// 원 반경
        /// </summary>
        private int circleRadius = 100;

        /// <summary>
        /// 타원 중심 X 좌표
        /// </summary>
        private int ellipseCenterX;

        /// <summary>
        /// 타원 중심 Y 좌표
        /// </summary>
        private int ellipseCenterY;

        /// <summary>
        /// 타원 반경 X
        /// </summary>
        private int ellipseRadiusX = 150;

        /// <summary>
        /// 타원 반경 Y
        /// </summary>
        private int ellipseRadiusY = 100;

        /// <summary>
        /// 타원 C 값
        /// </summary>
        private int ellipseC;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

            UpdateStyles();

            Load += Form_Load;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 배경 페인트시 처리하기 - OnPaintBackground(e)

        /// <summary>
        /// 배경 페인트시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            e.Graphics.Clear(Color.White);
        }

        #endregion
        #region 페인트시 처리하기 - OnPaint(e)

        /// <summary>
        /// 페인트시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            DrawCircle(e.Graphics);
        }

        #endregion
        #region 마우스 이동시 처리하기 - OnMouseMove(e)

        /// <summary>
        /// 마우스 이동시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            bool circleOver = false;
            {
                int x = Math.Abs(this.circleCenterX - e.X);
                int y = Math.Abs(this.circleCenterY - e.Y);

                if(Math.Sqrt(x * x + y * y) <= this.circleRadius)
                {
                    circleOver = true;
                }
            }

            bool ellipseOver = false;
            {
                int x;
                int y;

                double f1Length;
                double f2Length;

                if(this.ellipseRadiusX > this.ellipseRadiusY)
                {
                    x = Math.Abs((this.ellipseCenterX - this.ellipseC) - e.X);

                    y = Math.Abs(this.ellipseCenterY - e.Y);

                    f1Length = Math.Sqrt(x * x + y * y);

                    x = Math.Abs((this.ellipseCenterX + this.ellipseC) - e.X);

                    f2Length = Math.Sqrt(x * x + y * y);

                    ellipseOver = (f1Length + f2Length) <= (2 * this.ellipseRadiusX);
                }
                else
                {
                    y = Math.Abs((this.ellipseCenterY - this.ellipseC) - e.Y);

                    x = Math.Abs(e.X - this.ellipseCenterX);

                    f1Length = Math.Sqrt(x * x + y * y);

                    y = Math.Abs((this.ellipseCenterY + this.ellipseC) - e.Y);

                    f2Length = Math.Sqrt(x * x + y * y);

                    ellipseOver = (f1Length + f2Length) <= (2 * this.ellipseRadiusY);
                }
            }

            Text = string.Format("타원과 원 내부 마우스 위치 여부 구하기 (원 : {0}, 타원 : {1})", circleOver, ellipseOver);

            Invalidate();
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// 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)
        {
            this.circleCenterX = 150;
            this.circleCenterY = 150;

            this.ellipseCenterX = 300;
            this.ellipseCenterY = 250;

            if(this.ellipseRadiusX > this.ellipseRadiusY)
            {
                this.ellipseC = (int)Math.Sqrt(this.ellipseRadiusX * this.ellipseRadiusX - this.ellipseRadiusY * this.ellipseRadiusY);
            }
            else
            {
                this.ellipseC = (int)Math.Sqrt(this.ellipseRadiusY * this.ellipseRadiusY - this.ellipseRadiusX * this.ellipseRadiusX);
            }
        }

        #endregion

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

        #region 원 그리기 - DrawCircle(graphics)

        /// <summary>
        /// 원 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        private void DrawCircle(Graphics graphics)
        {
            Point mousePoint = PointToClient(Control.MousePosition);

            Rectangle circleRectangle = new Rectangle
            (
                this.circleCenterX - this.circleRadius,
                this.circleCenterY - this.circleRadius,
                this.circleRadius * 2,
                this.circleRadius * 2
            );

            graphics.DrawEllipse(Pens.Blue, circleRectangle);

            graphics.DrawLine(Pens.Violet, mousePoint, new Point(this.circleCenterX, this.circleCenterY));

            Rectangle ellipseRectangle = new Rectangle
            (
                this.ellipseCenterX - this.ellipseRadiusX,
                this.ellipseCenterY - this.ellipseRadiusY,
                this.ellipseRadiusX * 2,
                this.ellipseRadiusY * 2
            );

            graphics.DrawEllipse(Pens.Red, ellipseRectangle);

            Point focalPoint2;
            Point focalPoint1;

            if(this.ellipseRadiusX > this.ellipseRadiusY)
            {
                focalPoint1 = new Point(this.ellipseCenterX - this.ellipseC, this.ellipseCenterY);
                focalPoint2 = new Point(this.ellipseCenterX + this.ellipseC, this.ellipseCenterY);
            }
            else
            {
                focalPoint1 = new Point(this.ellipseCenterX, this.ellipseCenterY - this.ellipseC);
                focalPoint2 = new Point(this.ellipseCenterX, this.ellipseCenterY + this.ellipseC);
            }

            graphics.DrawLine(Pens.Violet, mousePoint, focalPoint1);
            graphics.DrawLine(Pens.Violet, mousePoint, focalPoint2);
        }

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

댓글을 달아 주세요