1.判断点是否在一群点内部
要判断一个点是否在一个由多个点围成的多边形内部(例如一圈点),可以使用射线法(Ray Casting Algorithm)来实现。以下是一个简单的 C# 实现示例
using System;
public class Point
{
public double X { get; set; }
public double Y { get; set; }
public Point(double x, double y)
{
X = x;
Y = y;
}
}
public class Program
{
public static bool IsPointInPolygon(Point testPoint, Point[] polygon)
{
bool inside = false;
int count = polygon.Length;
for (int i = 0, j = count - 1; i < count; j = i++)
{
if (((polygon[i].Y > testPoint.Y) != (polygon[j].Y > testPoint.Y)) &&
(testPoint.X < (polygon[j].X - polygon[i].X) * (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) + polygon[i].X))
{
inside = !inside;
}
}
return inside;
}
public static void Main()
{
Point[] polygon = new Point[]
{
new Point(