Is point inside polygon?

前端 未结 3 1798
-上瘾入骨i
-上瘾入骨i 2021-01-23 08:26

I need to write a function that will calculate if a point is inside polygon (true/false). Polygon always contains 4 points. I\'m reading polygons and points from SVG file

3条回答
  •  执念已碎
    2021-01-23 09:03

    Solved it this way:

    public static bool IsPointInPolygon4(PointF[] polygon, PointF testPoint)
        {
            bool result = false;
            int j = polygon.Count() - 1;
            for (int i = 0; i < polygon.Count(); i++)
            {
                if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y)
                {
                    if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X)
                    {
                        result = !result;
                    }
                }
                j = i;
            }
            return result;
        }
    

提交回复
热议问题