这段是网上的传入三维坐标和三维坐标集合就可以
///<summary>
/// 计算一个点是否在一个多边形范围内
/// 如果过该点的线段与多边形的交点不为零且距该点左右方向交点数量都为奇数时 该点再多边形范围内
/// <summary>
/// <param name="point">测试点</param>
/// <param name="vertexs">多边形的顶点集合</param>
/// <returns><returns>
public static bool PolygonIsContainPoint(Vector3 point, List<Vector3> vertexs)
{
//for (int i = 0; i < vertexs.Count; i++)
//{
// if (point.x == vertexs[i].x && point.z == vertexs[i].z)
// {
// return true;
// }
//}
//判断测试点和横坐标方向与多边形的边的交叉点
int leftNum = 0; //左方向上的交叉点数
int rightNum = 0; //右方向上的交叉点数
int index = 1;
for (int i = 0; i < vertexs.Count; i++)
{
if (i == vertexs.Count - 1) { index = -i; }
//找到相交的线段
if (point.z >= vertexs[i].z && point.z < vertexs[i + index].z || point.z < vertexs[i].z && point.z >= vertexs[i + index].z)
{
Vector3 vecNor = (vertexs[i + index] - vertexs[i]);
//处理直线方程为常数的情况
if (vecNor.x == 0.0f)
{
if (vertexs[i].x < point.x)
{
leftNum++;
}
else if (vertexs[i].x == point.x)
{ }
else
{
rightNum++;
}
}
else
{
vecNor = vecNor.normalized;
float k = vecNor.z / vecNor.x;
//print(vertexs[i + index]);
float b = vertexs[i].z - k * vertexs[i].x;
if ((point.z - b) / k < point.x)
{
leftNum++;
}
else if ((point.z - b) / k == point.x)
{
}
else
{
rightNum++;
}
}
}
}
if (leftNum % 2 != 0 || rightNum % 2 != 0)
{
return true;
}
return false;
}
计算边缘两边的点
/// <summary>
/// 边缘两边的点
/// </summary>
/// <param name="pointA"></param>
/// <param name="pointB"></param>
/// <param name="lineWidth"></param>
/// <param name="isOn"></param>
/// <returns></returns>
private List<Vector3> SidePoint(Vector3 pointA, Vector3 pointB, float lineWidth)
{
List<Vector3> points = new List<Vector3>();
//Debug.Log(pointA + ":" + pointB);
float HorDisABx = pointB.x - pointA.x;
float HorDisABz = pointB.z - pointA.z;
float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx, 2) + Mathf.Pow(HorDisABz, 2));
float offsetX = HorDisABz * lineWidth / HorDisAB;
float offsetZ = HorDisABx * lineWidth / HorDisAB;
Vector3 Point1 = new Vector3(pointA.x - offsetX, pointA.y, pointA.z + offsetZ);
Vector3 Point2 = new Vector3(pointA.x + offsetX, pointA.y, pointA.z - offsetZ);
Vector3 Point3 = new Vector3(pointB.x + offsetX, pointB.y, pointB.z - offsetZ);
Vector3 Point4 = new Vector3(pointB.x - offsetX, pointB.y, pointB.z + offsetZ);
//Vector3 centL = (Point1 + Point4) / 2;
//Vector3 centR = (Point2 + Point3) / 2;
//points.Add(centL);
//points.Add(centR);
points.Add(Point1);
points.Add(Point2);
points.Add(Point3);
points.Add(Point4);
// Debug.Log("Point0" + points[0]+ "Point1" + points[1]+"Point2" + points[2]+ "Point3" + points[3]);
return points;
}
传入二维坐标点计算
public static bool IsPointInPolygon(Vector2 p, List<Vector2> vertexs)
{
int crossNum = 0;
int vertexCount = vertexs.Count;
for (int i = 0; i < vertexCount; i++)
{
Vector2 v1 = vertexs[i];
Vector2 v2 = vertexs[(i + 1) % vertexCount];
if (((v1.y <= p.y) && (v2.y > p.y))
|| ((v1.y > p.y) && (v2.y <= p.y)))
{
if (p.x < v1.x + (p.y - v1.y) / (v2.y - v1.y) * (v2.x - v1.x))
{
crossNum += 1;
}
}
}
if (crossNum % 2 == 0)
{
return false;
}
else
{
return true;
}
}