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

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

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

using System.Collections.Generic;

 

List<DoublePoint> sourceList = new List<DoublePoint>();

 

sourceList.Add(new DoublePoint(50 , 50 ));

sourceList.Add(new DoublePoint(250, 50 ));

sourceList.Add(new DoublePoint(150, 250));

sourceList.Add(new DoublePoint(50 , 150));

sourceList.Add(new DoublePoint(50 , 50 ));

 

DoublePoint polygonCentroid = GetPolygonCentroid(sourceList);

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

 

■ 다각형 면적 구하기

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

using System.Collections.Generic;

 

#region 다각형 무게 중심 구하기 - GetPolygonCentroid(sourceList)

 

/// <summary>

/// 다각형 무게 중심 구하기

/// </summary>

/// <param name="sourceList">소스 리스트</param>

/// <returns>다각형 무게 중심</returns>

public DoublePoint GetPolygonCentroid(List<DoublePoint> sourceList)

{

    double centerX    = 0d;

    double centerY    = 0d;

    double polygonArea = 0d;

 

    int firstIndex;

    int secondIndex;

    int sourceCount = sourceList.Count;

         

    DoublePoint firstPoint;

    DoublePoint secondPoint;

     

    double factor = 0d;

 

    for(firstIndex = 0; firstIndex < sourceCount; firstIndex++)

    {

        secondIndex = (firstIndex + 1) % sourceCount;

             

        firstPoint  = sourceList[firstIndex ];

        secondPoint = sourceList[secondIndex];

             

        factor = ((firstPoint.X * secondPoint.Y) - (secondPoint.X * firstPoint.Y));

             

        polygonArea += factor;

             

        centerX += (firstPoint.X + secondPoint.X) * factor;

        centerY += (firstPoint.Y + secondPoint.Y) * factor;

    }

         

    polygonArea /= 2d;

    polygonArea *= 6d;

         

    factor = 1d / polygonArea;

         

    centerX *= factor;

    centerY *= factor;

         

    return new DoublePoint(centerX, centerY);

}

 

#endregion

 

/// <summary>

/// 실수 포인트

/// </summary>

public class DoublePoint

{

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

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

 

    #region X - X

 

    /// <summary>

    /// X

    /// </summary>

    public double X { get; set; }

 

    #endregion

    #region Y - Y

 

    /// <summary>

    /// Y

    /// </summary>

    public double Y { get; set; }

 

    #endregion

 

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

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

 

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

 

    /// <summary>

    /// 생성자

    /// </summary>

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

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

    public DoublePoint(double x, double y)

    {

        X = x;

        Y = y;

    }

 

    #endregion

 

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

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

 

    #region 문자열 구하기 - ToString()

 

    /// <summary>

    /// 문자열 구하기

    /// </summary>

    /// <returns>문자열</returns>

    public override string ToString()

    {

        return string.Format("X={0},Y={1}", X, Y);

    }

 

    #endregion

}

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

Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요