1 回答
TA贡献1828条经验 获得超4个赞
解决方案是找到到多边形的最近距离,如果距离在边距内,则返回 true,以下是完整代码:
'''
public static float DistancePointLine2D(Vector2 point, Vector2 lineStart, Vector2 lineEnd)
{
return (ProjectPointLine2D(point, lineStart, lineEnd) - point).magnitude;
}
public static Vector2 ProjectPointLine2D(Vector2 point, Vector2 lineStart, Vector2 lineEnd)
{
Vector2 rhs = point - lineStart;
Vector2 vector2 = lineEnd - lineStart;
float magnitude = vector2.magnitude;
Vector2 lhs = vector2;
if (magnitude > 1E-06f)
{
lhs = (Vector2)(lhs / magnitude);
}
float num2 = Mathf.Clamp(Vector2.Dot(lhs, rhs), 0f, magnitude);
return (lineStart + ((Vector2)(lhs * num2)));
}
public static float ClosestDistanceToPolygon(Vector2[] verts, Vector2 point)
{
int nvert = verts.Length;
int i, j = 0;
float minDistance = Mathf.Infinity;
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
float distance = DistancePointLine2D(point, verts[i], verts[j]);
minDistance = Mathf.Min(minDistance, distance);
}
return minDistance;
}
public static bool IsInsidePolygon(Vector2[] vertices, Vector2 checkPoint, float margin = 0.01f)
{
if(ClosestDistanceToPolygon(vertices, checkPoint) < margin)
{
return true;
}
float[] vertX = new float[vertices.Length];
float[] vertY = new float[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
vertX[i] = vertices[i].x;
vertY[i] = vertices[i].y;
}
return IsInsidePolygon(vertices.Length, vertX, vertY, checkPoint.x, checkPoint.y);
}
public static bool IsInsidePolygon(int nvert, float[] vertx, float[] verty, float testx, float testy)
{
bool c = false;
int i, j = 0;
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if ((((verty[i] <= testy) && (testy < verty[j])) ||
((verty[j] <= testy) && (testy < verty[i]))) &&
(testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))
c = !c;
}
return c;
}
'''
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报