첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

▶ 다각형 내부 위치 여부 구하기 예제

using System;
using System.Collections.Generic;

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

sourceList.Add(new DoublePoint(100, 100));
sourceList.Add(new DoublePoint(150, 130));
sourceList.Add(new DoublePoint(120, 170));
sourceList.Add(new DoublePoint(90 , 150));
sourceList.Add(new DoublePoint(100, 100));

DoublePoint targetPoint = new DoublePoint(100, 150);

bool result = IsPointInPolygon(sourceList, targetPoint);

Console.WriteLine(result ? "내부 위치" : "외부 위치");

 

728x90

 

▶ 다각형 내부 위치 여부 구하기

using System.Collections.Generic;

#region 다각형 내부 위치 여부 구하기 - IsPointInPolygon(sourceList, targetPoint)

/// <summary>
/// 다각형 내부 위치 여부 구하기
/// </summary>
/// <param name="sourceList">소스 리스트</param>
/// <param name="targetPoint">타겟 포인트</param>
/// <returns>다각형 내부 위치 여부</returns>
public bool IsPointInPolygon(List<DoublePoint> sourceList, DoublePoint targetPoint)
{
    bool result = false;

    int j = sourceList.Count - 1;

    for(int i = 0; i < sourceList.Count; i++)
    {
        if
        (
            sourceList[i].Y < targetPoint.Y && sourceList[j].Y >= targetPoint.Y ||
            sourceList[j].Y < targetPoint.Y && sourceList[i].Y >= targetPoint.Y
        )
        {
            double value = sourceList[i].X + (targetPoint.Y - sourceList[i].Y) /
               (sourceList[j].Y - sourceList[i].Y) * (sourceList[j].X - sourceList[i].X);

            if(value < targetPoint.X)
            {
                result =! result;
            }
        }

        j = i;
    }

    return result;
}

#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

댓글을 달아 주세요