39 lines
1.3 KiB
C#
39 lines
1.3 KiB
C#
using WorkerService1.Domains;
|
|
|
|
namespace WorkerService1.Utils;
|
|
|
|
public static class Utilities
|
|
{
|
|
public static bool IsPointInsidePolygon(this Points point, Polygon polygon)
|
|
{
|
|
// 使用射线交叉法判断点是否在多边形内
|
|
int count = 0;
|
|
double x0 = point.X;
|
|
double y0 = point.Y;
|
|
for (int i = 0; i < polygon.Points.Count; i++)
|
|
{
|
|
var currentPolygon = polygon.Points[i];
|
|
for (int i2 = 0; i2 < currentPolygon.Count; i2++)
|
|
{
|
|
int next = (i2 + 1) % currentPolygon.Count;
|
|
int prev = (i2 - 1 + currentPolygon.Count) % currentPolygon.Count;
|
|
double x1 = currentPolygon[next].X;
|
|
double y1 = currentPolygon[next].Y;
|
|
double x2 = currentPolygon[prev].X;
|
|
double y2 = currentPolygon[prev].Y;
|
|
if (((y1 > y0) && (y2 <= y0)) || ((y2 > y0) && (y1 <= y0)))
|
|
{
|
|
double intersection = (x1 - x2) * (y0 - y2) / (y1 - y2) + x2;
|
|
if (x0 < intersection)
|
|
{
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (count % 2 == 1) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |