Python判断一个点或者凸多边形是否在一个凸四边形内部

参考这里,稍微做了一些改动。

def isInterArea(testPoint, AreaPoint):
    # testPoint为待测点[x,y]或者任意凸多边形[x1, y1, ...]
    # AreaPoint为按顺时针顺序的4个点[x1,y1,x2,y2,x3,y3,x4,y4]
    if len(testPoint) > 2:
        for i in range(0, len(testPoint), 2):
            if not isInterArea(testPoint[i:i+2], AreaPoint):
                return False
        return True

    LBPoint = AreaPoint[0:2]
    LTPoint = AreaPoint[2:4]
    RTPoint = AreaPoint[4:6]
    RBPoint = AreaPoint[6:8]
    a = (LTPoint[0]-LBPoint[0])*(testPoint[1]-LBPoint[1])-(LTPoint[1]-LBPoint[1])*(testPoint[0]-LBPoint[0])
    b = (RTPoint[0]-LTPoint[0])*(testPoint[1]-LTPoint[1])-(RTPoint[1]-LTPoint[1])*(testPoint[0]-LTPoint[0])
    c = (RBPoint[0]-RTPoint[0])*(testPoint[1]-RTPoint[1])-(RBPoint[1]-RTPoint[1])*(testPoint[0]-RTPoint[0])
    d = (LBPoint[0]-RBPoint[0])*(testPoint[1]-RBPoint[1])-(LBPoint[1]-RBPoint[1])*(testPoint[0]-RBPoint[0])
    # print(a,b,c,d)
    if (a>=0 and b>=0 and c>=0 and d>=0) or (a<=0 and b<=0 and c<=0 and d<=0):
        return True
    else:
        return False

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值