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

▶ 다각형 면적 구하기 예제

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

double polygonArea = GetPolygonArea(sourceList);

 

728x90

 

▶ 다각형 면적 구하기

using System.Collections.Generic;

#region 다각형 면적 구하기 - GetPolygonArea(sourceList)

/// <summary>
/// 다각형 면적 구하기
/// </summary>
/// <param name="sourceList">소스 리스트</param>
/// <returns>다각형 면적</returns>
public double GetPolygonArea(List<DoublePoint> sourceList)
{
    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;
    }
         
    polygonArea /= 2d;

    return polygonArea;
}

#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
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요