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
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] 팩토리얼 구하기 (0) | 2014.11.30 |
---|---|
[C#/COMMON] 네트워크 드라이브 연결 끊기 (0) | 2014.11.30 |
[C#/COMMON] 네트워크 드라이브 연결하기 (0) | 2014.11.30 |
[C#/COMMON] MethodBase 클래스 : GetCurrentMethod 정적 메소드를 사용해 현재 실행 중인 메소드명 구하기 (0) | 2014.11.30 |
[C#/COMMON] 최소 제곱법을 사용해 회귀분석 수식 구하기 (0) | 2014.11.30 |
[C#/COMMON] 시작 애플리케이션 설정하기 (0) | 2014.11.30 |
[C#/COMMON] Process 클래스 : Start 메소드를 사용해 커맨드 창 실행하기 (0) | 2014.11.30 |
[C#/COMMON] Process 클래스 : Start 메소드를 사용해 인터넷 익스플로러 실행하기 (0) | 2014.11.30 |
[C#/COMMON] 조합 정렬하기 (0) | 2014.11.30 |
[C#/COMMON] 칵테일 정렬하기 (0) | 2014.11.30 |
댓글을 달아 주세요