简体   繁体   English

c#检查多边形中是否存在点

[英]c# Check if point exist inside a polygon

Hi I am looking to find a way of checking if a point exists inside a polygon from my c# code.I successfully implemented the same with javascript using Google maps geometry library as reference from this link .Now I need to do the same from c# code.Is google providing any webservices or dll for the same? 嗨,我正在寻找一种方法来检查我的C#代码中的多边形内是否存在一个点。我成功地使用Google Maps几何库作为此链接的引用,使用javascript实现了相同的代码。现在我需要从c#代码中执行相同的操作谷歌是否提供相同的任何webservices或dll?

If not, can anyone suggest any third party api's or plugin's for the same purpose.I already tried some third party item's but didn't find any one giving accuracy like Google maps geometry library 如果不是这样的话,有人可以出于相同的目的建议任何第三方的api或插件。我已经尝试了一些第三方的api或插件,但是找不到像Google Maps geometry库这样的具有准确性的东西

This problem is trivial to program. 这个问题对程序来说微不足道。

To simplify, first assume the test point to be at the origin. 为简化起见,首先假定测试点位于原点。 You need to check the parity of the number of intersections of the positive X-axis with the polygon edges. 您需要检查正X轴与多边形边的相交数量的奇偶性。

Inside= False
for k in in 0..N-1:
    if (Y[k] > 0) != (Y[k+1] > 0):
        # The edge straddles the X-axis...
        if (Y[k] > 0) == (Y[k] * X[k+1] > Y[k+1] * X[k]):
            # ... and intersects it on the positive side
            Inside= not Inside

Take the indexes modulo N , so that N≡0 . N为模的索引,使N≡0

For a test point not at the origin, it suffices to translate the coordinates of all polygon vertices, which you do on the fly without actually modifying the polygon. 对于不在原点上的测试点,只需平移所有多边形顶点的坐标即可,您可以在不实际修改多边形的情况下即时进行此操作。

在此处输入图片说明

If the polygon crosses the 180° meridian, repeat the point-in-polygon test with the longitude ± 360°. 如果多边形跨过180°子午线,请以±360°的经度重复多边形中点测试。

private static bool EstaDentroDeZona(double latitudActual, double longitudActual, List<PuntoPorZona> listaPuntosPorZona)
    {
        Punto<double> vector1 = new Punto<double>();
        Punto<double> vector2 = new Punto<double>();



        int i = 0;
        double a = 0;

        for (i = 0; i < listaPuntosPorZona.Count - 1; i++)
        {
            vector1.X = Convert.ToDouble(listaPuntosPorZona[i].Latitud) - latitudActual;
            vector1.Y = Convert.ToDouble(listaPuntosPorZona[i].Longitud) - longitudActual;
            vector2.X = Convert.ToDouble(listaPuntosPorZona[i + 1].Latitud) - latitudActual;
            vector2.Y = Convert.ToDouble(listaPuntosPorZona[i + 1].Longitud) - longitudActual;

            a = a + Angulo(vector1, vector2);
        }

        double grados = a * 180 / Math.PI;
        return Math.Abs(grados) > 180;
    }

    private static double Angulo(Punto<double> v1, Punto<double> v2)
    {
        double angulo;
        double calculoModulos;
        double calculoProductoEscalar;
        calculoModulos = Modulo(v1) * Modulo(v2);
        calculoProductoEscalar = ProductoEscalar(v1, v2);

        if (ModuloDelProductoVectorialConSigno(v1, v2) > 0)
        {
            angulo = Math.Acos(calculoProductoEscalar / calculoModulos);
        }
        else
        {
            angulo = -1 * (Math.Acos(calculoProductoEscalar / calculoModulos));
        }
        return angulo;
    }

    private static double ProductoEscalar(Punto<double> punto1, Punto<double> punto2)
    {
        double productoEscalcar;
        productoEscalcar = (punto1.X * punto2.X) + (punto1.Y * punto2.Y);
        return productoEscalcar;
    }

    private static double Modulo(Punto<double> punto)
    {
        double modulo;
        modulo = Math.Sqrt(punto.X * punto.X + punto.Y * punto.Y);
        return modulo;
    }

    private static double ModuloDelProductoVectorialConSigno(Punto<double> v1, Punto<double> v2)
    {
        double angulo = ((v1.X * v2.Y) - (v1.Y * v2.X));
        return angulo;
    }

    private struct Punto<T>
    {
        T x;
        T y;

        public Punto(T a, T b)
        {
            x = a;
            y = b;
        }

        public T X
        {
            get { return x; }
            set { x = value; }
        }

        public T Y
        {
            get { return y; }
            set { y = value; }
        }
    }

Just translated to English methods 刚翻译成英文的方法

listaPuntoPorZona = Points of Polygon listaPuntoPorZona =多边形点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM