由點發出的射線與多邊形邊的交點個數,若是是偶數個說明在多邊形的外面,交點個數爲奇數個在多邊形的內部,下面是代碼:ide
public bool IsPointInPolygon(Vector2 point, Vector2[] polygon) { int polygonLength = polygon.Length, i = 0; bool inside = false; float pointX = point.x, pointY = point.y; float startX, startY, endX, endY; Vector2 endPoint = polygon[polygonLength - 1]; endX = endPoint.x; endY = endPoint.y; while (i < polygonLength) { startX = endX; startY = endY; endPoint = polygon[i++]; endX = endPoint.x; endY = endPoint.y; inside ^= (endY > pointY ^ startY > pointY) && ((pointX - endX) < (pointY - endY) * (startX - endX) / (startY - endY)); } return inside; }