private bool IsInsidePolygon(Vector3 point, IReadOnlyCollection<Vector3> polygonVertices)
{
var count = polygonVertices.Count;
var centroid = GetCentroid(polygonVertices);
// Sort vertices based on angle from centroid
var sortedVertices = polygonVertices.OrderBy(v => Mathf.Atan2(v.z - centroid.z, v.x - centroid.x)).ToList();
var inside = false;
for (int i = 0, j = count - 1; i < count; j = i++)
{
if (sortedVertices[i].z > point.z == sortedVertices[j].z > point.z ||
!(point.x < (sortedVertices[j].x - sortedVertices[i].x) * (point.z - sortedVertices[i].z) / (sortedVertices[j].z - sortedVertices[i].z) + sortedVertices[i].x))
continue;
if (point.y <= Height && point.y >= Min)
inside = !inside;
}
return inside;
}
private Vector3 GetCentroid(IReadOnlyCollection<Vector3> vertices)
{
var centroid = new Vector3(0, 0, 0);
vertices.ToList().ForEach(v => centroid += v);
return centroid / vertices.Count;
}
private bool IsInsidePolygon(Vector3 point, IReadOnlyCollection<Vector3> polygonVertices)
{
var count = polygonVertices.Count;
var centroid = GetCentroid(polygonVertices);
// Sort vertices based on angle from centroid
var sortedVertices = polygonVertices.OrderBy(v => Mathf.Atan2(v.z - centroid.z, v.x - centroid.x)).ToList();
var inside = false;
for (int i = 0, j = count - 1; i < count; j = i++)
{
if (sortedVertices[i].z > point.z == sortedVertices[j].z > point.z ||
!(point.x < (sortedVertices[j].x - sortedVertices[i].x) * (point.z - sortedVertices[i].z) / (sortedVertices[j].z - sortedVertices[i].z) + sortedVertices[i].x))
continue;
if (point.y <= Height && point.y >= Min)
inside = !inside;
}
return inside;
}
private Vector3 GetCentroid(IReadOnlyCollection<Vector3> vertices)
{
var centroid = new Vector3(0, 0, 0);
vertices.ToList().ForEach(v => centroid += v);
return centroid / vertices.Count;
}