How can I determine whether a 2D Point is within a Polygon?

前端 未结 30 2153
醉梦人生
醉梦人生 2020-11-21 05:06

I\'m trying to create a fast 2D point inside polygon algorithm, for use in hit-testing (e.g. Polygon.contains(p:Point)). Suggestions for effective tech

30条回答
  •  不知归路
    2020-11-21 05:57

    .Net port:

        static void Main(string[] args)
        {
    
            Console.Write("Hola");
            List vertx = new List();
            List verty = new List();
    
            int i, j, c = 0;
    
            vertx.Add(1);
            vertx.Add(2);
            vertx.Add(1);
            vertx.Add(4);
            vertx.Add(4);
            vertx.Add(1);
    
            verty.Add(1);
            verty.Add(2);
            verty.Add(4);
            verty.Add(4);
            verty.Add(1);
            verty.Add(1);
    
            int nvert = 6;  //Vértices del poligono
    
            double testx = 2;
            double testy = 5;
    
    
            for (i = 0, j = nvert - 1; i < nvert; j = i++)
            {
                if (((verty[i] > testy) != (verty[j] > testy)) &&
                 (testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))
                    c = 1;
            }
        }
    

提交回复
热议问题