经纬坐标点的多边形区域判断

经纬坐标点的多边形区域判断

python 实现,封装成函数,

def is_pt_in_poly(x, y, point_list, point_list_size):
isum = 0.0
icount = point_list_size

for i in range(icount//2):
    # 选取各段直线
    # 1) 确定索引
    # 2)根据索引确定边界线段
    parallel_weight = 0

    if i < icount // 2 - 2:
        index_n1 = i - 1
        index_a1 = i + 1
        index_a2 = i + 2

    elif i == icount // 2 - 2:
        index_n1 = i - 1
        index_a1 = i + 1
        index_a2 = 0

    elif i == icount // 2 - 1:
        index_n1 = i - 1
        index_a1 = 0
        index_a2 = 1

    x_0 = point_list[i * 2]
    y_0 = point_list[i * 2 + 1]
    x_n1 = point_list[index_n1 * 2]
    y_n1 = point_list[index_n1 * 2 + 1]
    x_a1 = point_list[index_a1 * 2]
    y_a1 = point_list[index_a1 * 2 + 1]
    x_a2 = point_list[index_a2 * 2]
    y_a2 = point_list[index_a2 * 2 + 1]

    # 计算判定的点与各线段的交点
    if ( y >= min(y_0, y_a1) ) & ( y <= max(y_0, y_a1) ) :

        # 1、点重叠顶点 
        if ( x == x_0 ) & (y == y_0) :
            return  1
        
        # 2、点过顶点
        elif y == y_0 :
            # 边界水平
            if y_0 == y_a1:
                if x < min(x_0, x_a1):
                    if y_0 - y_n1 > 0:
                        parallel_weight += 0.5
                    elif y_0 - y_n1 < 0:
                        parallel_weight += -0.5
                    else:
                        parallel_weight += 0

                    if y_a1 - y_0 > 0:
                        parallel_weight += 0.5
                    elif y_a1 - y_0 < 0:
                        parallel_weight += -0.5
                    else:
                        parallel_weight += 0

                    if y_a2 - y_a1 > 0:
                        parallel_weight += 0.5
                    elif y_a2 - y_a1 < 0:
                        parallel_weight += -0.5
                    else:
                        parallel_weight += 0
                elif x > max(x_0, x_a1):
                    pass
                else:
                    return 1
            # 边界不水平
            else:
                if y_n1 != y:
                    if x < x_0:
                        if ( y_0 - y_n1 ) * ( y_a1 - y_0) > 0:
                            isum += 1 
                        else:
                            pass
                    elif x > x_0:
                        pass
                else:
                    pass
            
        # 3、点过尾点
        elif  y == y_a1 :
            pass

        # 4、点过边界 (边界垂直和一般情况)
        else:
            if x_0 == x_a1:
                if x < x_0 :
                    isum += 1
                elif x > x_0:
                    pass
                else:
                    return 1 # 在线段上 
            else:
                line_poin_x = x - (x_a1 * y - x_0 * y - x_a1 * y_0 + x_0 * y_a1) / (y_a1 - y_0)
                if line_poin_x < 0:
                    isum += 1
                elif line_poin_x > 0:
                    pass 
                else :
                    return 1
    else:
        pass

    isum = isum + abs(parallel_weight)

if isum % 2 == 0:
    return 0
else:
    return 1

 [1]: https://wenku.baidu.com/view/5e3913a2b0717fd5360cdccf.html?qq-pf-to=pcqq.c2c
 [2]: https://blog.csdn.net/qq_22929803/article/details/46818009

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页