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

■ 삼각형 무게 중심 구하기 예제

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


TestProject.zip


DoublePoint.cs

 

 

using System;

using System.Drawing;

 

namespace TestProject

{

    /// <summary>

    /// 실수형 포인트

    /// </summary>

    public struct DoublePoint

    {

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

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

 

        #region Field

 

        /// <summary>

        /// X 좌표

        /// </summary>

        private double x;

 

        /// <summary>

        /// Y 좌표

        /// </summary>

        private double y;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

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

 

        #region X 좌표 - X

 

        /// <summary>

        /// X 좌표

        /// </summary>

        public double X

        {

            get

            {

                return this.x;

            }

            set

            {

                this.x = value;

            }

        }

 

        #endregion

        #region Y 좌표 - Y

 

        /// <summary>

        /// Y 좌표

        /// </summary>

        public double Y

        {

            get

            {

                return this.y;

            }

            set

            {

                this.y = value;

            }

        }

 

        #endregion

 

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

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

 

        #region 생성자 - DoublePoint(x, y)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="x">X 좌표</param>

        /// <param name="y">Y 좌표</param>

        public DoublePoint(double x, double y)

        {

            this.x = x;

            this.y = y;

        }

 

        #endregion

 

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

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

 

        #region 포인트 구하기 - GetPoint()

 

        /// <summary>

        /// 포인트 구하기

        /// </summary>

        /// <returns>포인트</returns>

        public Point GetPoint()

        {

            return new Point(Convert.ToInt32(this.x), Convert.ToInt32(this.y));

        }

 

        #endregion

        #region 사각형 구하기 - GetRectangle(width, height)

 

        /// <summary>

        /// 사각형 구하기

        /// </summary>

        /// <param name="width">너비</param>

        /// <param name="height">높이</param>

        /// <returns>사각형</returns>

        public Rectangle GetRectangle(int width, int height)

        {

            return new Rectangle

            (

                Convert.ToInt32(this.x) - width  / 2,

                Convert.ToInt32(this.y) - height / 2,

                width,

                height

            );

        }

 

        #endregion    

    }

}

 

 

Triangle.cs

 

 

using System.Drawing;

 

namespace TestProject

{

    /// <summary>

    /// 삼각형

    /// </summary>

    public class Triangle

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 꼭지점1

        /// </summary>

        private DoublePoint vertex1;

 

        /// <summary>

        /// 꼭지점2

        /// </summary>

        private DoublePoint vertex2;

 

        /// <summary>

        /// 꼭지점3

        /// </summary>

        private DoublePoint vertex3;

 

        /// <summary>

        /// 중간점1

        /// </summary>

        /// <remarks>

        /// 꼭지점1과 꼭지점2 사이 중간점

        /// </remarks>

        private DoublePoint medianPoint1;

 

        /// <summary>

        /// 중간점2

        /// </summary>

        /// <remarks>

        /// 꼭지점2과 꼭지점3 사이 중간점

        /// </remarks>

        private DoublePoint medianPoint2;

 

        /// <summary>

        /// 중간점3

        /// </summary>

        /// <remarks>

        /// 꼭지점3과 꼭지점1 사이 중간점

        /// </remarks>

        private DoublePoint medianPoint3;

 

        /// <summary>

        /// 무게 중심점

        /// </summary>

        private DoublePoint gravityCenterPoint;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

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

 

        #region 꼭지점1 - Vertex1

 

        /// <summary>

        /// 꼭지점1

        /// </summary>

        public DoublePoint Vertex1

        {

            get

            {

                return this.vertex1;

            }

        }

 

        #endregion

        #region 꼭지점2 - Vertex2

 

        /// <summary>

        /// 꼭지점2

        /// </summary>

        public DoublePoint Vertex2

        {

            get

            {

                return this.vertex2;

            }

        }

 

        #endregion

        #region 꼭지점3 - Vertex3

 

        /// <summary>

        /// 꼭지점3

        /// </summary>

        public DoublePoint Vertex3

        {

            get

            {

                return this.vertex3;

            }

        }

 

        #endregion

 

        #region 중간점1 - MedianPoint1

 

        /// <summary>

        /// 중간점1

        /// </summary>

        /// <remarks>꼭지점1과 꼭지점2 사이 중간점</remarks>

        public DoublePoint MedianPoint1

        {

            get

            {

                return this.medianPoint1;

            }

        }

 

        #endregion

        #region 중간점2 - MedianPoint2

 

        /// <summary>

        /// 중간점2

        /// </summary>

        /// <remarks>꼭지점2과 꼭지점3 사이 중간점</remarks>

        public DoublePoint MedianPoint2

        {

            get

            {

                return this.medianPoint2;

            }

        }

 

        #endregion

        #region 중간점3 - MedianPoint3

 

        /// <summary>

        /// 중간점3

        /// </summary>

        /// <remarks>꼭지점3과 꼭지점1 사이 중간점</remarks>

        public DoublePoint MedianPoint3

        {

            get

            {

                return this.medianPoint3;

            }

        }

 

        #endregion

 

        #region 무게 중심점 - GravityCenterPoint

 

        /// <summary>

        /// 무게 중심점

        /// </summary>

        public DoublePoint GravityCenterPoint

        {

            get

            {

                return this.gravityCenterPoint;

            }

        }

 

        #endregion

 

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

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

 

        #region 생성자 - Triangle(vertex1, vertex2, vertex3)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="vertex1">꼭지점1</param>

        /// <param name="vertex2">꼭지점2</param>

        /// <param name="vertex3">꼭지점3</param>

        public Triangle(DoublePoint vertex1, DoublePoint vertex2, DoublePoint vertex3)

        {

            this.vertex1 = vertex1;

            this.vertex2 = vertex2;

            this.vertex3 = vertex3;

 

            this.medianPoint1 = GetMedianPoint(this.vertex1, this.vertex2);

            this.medianPoint2 = GetMedianPoint(this.vertex2, this.vertex3);

            this.medianPoint3 = GetMedianPoint(this.vertex3, this.vertex1);

 

            this.gravityCenterPoint = GetGravityCetnerPoint(this.vertex2, this.vertex3, this.medianPoint1, this.medianPoint3);

        }

 

        #endregion

 

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

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

 

        #region 그리기 - Draw(graphics)

 

        /// <summary>

        /// 그리기

        /// </summary>

        /// <param name="graphics">Graphics</param>

        public void Draw(Graphics graphics)

        {

            graphics.DrawLine(Pens.Blue, this.vertex1.GetPoint(), this.vertex2.GetPoint());

            graphics.DrawLine(Pens.Blue, this.vertex2.GetPoint(), this.vertex3.GetPoint());

            graphics.DrawLine(Pens.Blue, this.vertex3.GetPoint(), this.vertex1.GetPoint());

 

            graphics.FillEllipse(Brushes.DarkGreen, this.medianPoint1.GetRectangle(10, 10));

            graphics.FillEllipse(Brushes.DarkGreen, this.medianPoint2.GetRectangle(10, 10));

            graphics.FillEllipse(Brushes.DarkGreen, this.medianPoint3.GetRectangle(10, 10));

 

            graphics.DrawLine(Pens.Gray, this.vertex1.GetPoint(), this.medianPoint2.GetPoint());

            graphics.DrawLine(Pens.Gray, this.vertex2.GetPoint(), this.medianPoint3.GetPoint());

            graphics.DrawLine(Pens.Gray, this.vertex3.GetPoint(), this.medianPoint1.GetPoint());

 

            graphics.FillEllipse(Brushes.Red, this.gravityCenterPoint.GetRectangle(10, 10));

        }

 

        #endregion

 

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

 

        #region 중간점 구하기 - GetMedianPoint(point1, point2)

 

        /// <summary>

        /// 중간점 구하기

        /// </summary>

        /// <param name="point1">점1</param>

        /// <param name="point2">점2</param>

        /// <returns>중간점</returns>

        /// <remarks>점1과 점2를 직선으로 연결했을 때의 중간점을 구한다.</remarks>

        private DoublePoint GetMedianPoint(DoublePoint point1, DoublePoint point2)

        {

            return new DoublePoint((point1.X + point2.X) / 2d, (point1.Y + point2.Y) / 2d);

        }

 

        #endregion

        #region 기울기 구하기 - GetGradien(point1, point2)

 

        /// <summary>

        /// 기울기 구하기

        /// </summary>

        /// <param name="point1">점1</param>

        /// <param name="point2">점2</param>

        /// <returns>점1과 점2를 직선으로 연결했을 때의 1차 방정식 기울기를 구한다.</returns>

        private double GetGradien(DoublePoint point1, DoublePoint point2)

        {

            return (point1.Y - point2.Y) / (point1.X - point2.X);

        }

 

        #endregion

        #region Y 절편 구하기 - GetYIntersept(point1, point2)

 

        /// <summary>

        /// Y 절편 구하기

        /// </summary>

        /// <param name="point1">점1</param>

        /// <param name="point2">점2</param>

        /// <returns>Y 절편</returns>

        /// <remarks>점1과 점2를 직선으로 연결했을 때의 1차 방정식 Y 절편을 구한다.</remarks>

        private double GetYIntersept(DoublePoint point1, DoublePoint point2)

        {

            return -point1.X * ((point1.Y - point2.Y) / (point1.X - point2.X)) + point1.Y;

        }

 

        #endregion

        #region 무게 중심점 구하기 - GetGravityCetnerPoint()

 

        /// <summary>

        /// 무게 중심점 구하기

        /// </summary>

        /// <param name="vertex2">꼭지점2</param>

        /// <param name="vertex3">꼭지점3</param>

        /// <param name="medianPoint1">중간점1</param>

        /// <param name="medianPoint3">중간점3</param>

        /// <returns>무게 중심점</returns>

        /// <remarks>

        /// 중간점1 : 꼭지점1과 꼭지점2 사이 중간점

        /// 중간점3 : 꼭지점3과 꼭지점1 사이 중간점

        /// </remarks>

        private DoublePoint GetGravityCetnerPoint(DoublePoint vertex2, DoublePoint vertex3, DoublePoint medianPoint1,

            DoublePoint medianPoint3)

        {

            double gradient2   = GetGradien   (vertex2, medianPoint3); // 꼭지점2에서 중간점3을 연결하는 직선의 기울기

            double yIntercept2 = GetYIntersept(vertex2, medianPoint3); // 꼭지점2에서 중간점3을 연결하는 직선의 Y 절편

 

            double gradient3   = GetGradien   (vertex3, medianPoint1); // 꼭지점3에서 중간점1을 연결하는 직선의 기울기

            double yIntercept3 = GetYIntersept(vertex3, medianPoint1); // 꼭지점3에서 중간점1을 연결하는 직선의 Y 절편

 

            double x = (-yIntercept2 + yIntercept3) / (gradient2 - gradient3);                                       // 무게 중심 X

            double y = (-gradient2 * yIntercept2 + yIntercept3 * gradient2) / (gradient2 - gradient3) + yIntercept2; // 무게 중심 Y

 

            return new DoublePoint(x, y);

        }

 

        #endregion

    }

}

 

 

MainForm.cs

 

 

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 삼각형

        /// </summary>

        private Triangle triangle = null;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.triangle = new Triangle(new DoublePoint(300, 50), new DoublePoint(50, 400), new DoublePoint(450, 200));

 

            Paint += Form_Paint;

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

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

        /// </summary>

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

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

        private void Form_Paint(object sender, PaintEventArgs e)

        {

            this.triangle.Draw(e.Graphics);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요