判断平面内矩形和圆是否有交点的python实现

#! /usr/bin/env python 
# -*- coding: utf-8 -*-

'''
@filename : 平面坐标内矩形与圆是否相交
@usage    : 
@authors  : U{peterguo<mailto: peterguo@tencent.com>}
@copyright: tencent
@date     : 2012.08.21
@version  : 1.0.0.0

Modified History:
   2012.08.21 创建
'''

import os
import sys
import re
import urllib2
import time
import datetime
import math

def usage():
    '''
    用法说明
    '''
    sys.exit(1)
    
def bPointInCircle(fPointX, fPointY, fCircleX, fCircleY, fRadius):
    oPoint = complex(fPointX, fPointY)
    oCircle = complex(fCircleX, fCircleY)
    if abs(oPoint - oCircle) < fRadius:
        return True
    else:
        return False
        
def bPointInRectangle(fPointX, fPointY, fRectTopLeftX, fRectTopLeftY, fRectBottomRightX, fRectBottomRightY):
    if not (fRectTopLeftX <= fPointX <= fRectBottomRightX or fRectTopLeftX >= fPointX >= fRectBottomRightX):
        return False
    if not (fRectTopLeftY <= fPointY <= fRectBottomRightY or fRectTopLeftY >= fPointY >= fRectBottomRightY):
        return False
    return True

def bCircleIntersectRectangle(fRectTopLeftX=0, fRectTopLeftY=10, fRectBottomRightX=20, fRectBottomRightY=0, #矩形左上点和右下点坐标
                              fCircleX=0, fCircleY=0,   #圆心
                              fRadius=10):             #圆半径
                              
    o1 = complex(fRectTopLeftX, fRectTopLeftY)
    o4 = complex(fRectBottomRightX, fRectBottomRightY)
    
    o2 = complex(fRectBottomRightX, fRectTopLeftY)
    o3 = complex(fRectTopLeftX, fRectBottomRightY)
    
    bO1 = bPointInCircle(fCircleX, fCircleY, o1.real, o1.imag, fRadius)
    bO2 = bPointInCircle(fCircleX, fCircleY, o2.real, o2.imag, fRadius)
    bO3 = bPointInCircle(fCircleX, fCircleY, o3.real, o3.imag, fRadius)
    bO4 = bPointInCircle(fCircleX, fCircleY, o4.real, o4.imag, fRadius)
    
    bABCD = bPointInRectangle(fCircleX, fCircleY, o1.real, o1.imag + fRadius, o2.real, o2.imag - fRadius)
    bEFGH = bPointInRectangle(fCircleX, fCircleY, o3.real, o3.imag + fRadius, o4.real, o4.imag - fRadius)
    
    bIJKL = bPointInRectangle(fCircleX, fCircleY, o1.real - fRadius, o1.imag, o3.real + fRadius, o3.imag)
    bMNOP = bPointInRectangle(fCircleX, fCircleY, o2.real - fRadius, o2.imag, o4.real + fRadius, o4.imag)
    
    #print bO1 , bO2 , bO3 , bO4 , bABCD , bEFGH , bIJKL , bMNOP
    return bO1 or bO2 or bO3 or bO4 or bABCD or bEFGH or bIJKL or bMNOP
    
if __name__ == "__main__":

    print "False", bPointInCircle(0,0, 100, 100, 100)
    print "False", bPointInCircle(0,0, 100, 100, 10)
    print "True", bPointInCircle(0,0, 0, 0, 10)
    print "True", bPointInRectangle(0, 0, -1, -2, 3, 4)
    print "False", bPointInRectangle(0, 0, 1, 2, 3, 4)
    print "True", bCircleIntersectRectangle()
    print "False", bCircleIntersectRectangle(2.0, 4.0, 4.5, 1.0, 1.0, 2.0, 1.0)
    print "True", bCircleIntersectRectangle(2.0, 4.0, 4.5, 1.0, 1.0, 2.0, 2.0)
    print "False", bCircleIntersectRectangle(2.0, 4.0, 4.5, 1.0, 100.0, 2.0, 2.0)
    print "True", bCircleIntersectRectangle(2.0, 4.0, 4.5, 1.0, 3.0, 3.0, 1.0)
    print "False", bCircleIntersectRectangle(2.0, 4.0, 4.5, 1.0, 3.0, 3.0, 0.5)


转载于:https://my.oschina.net/sanpeterguo/blog/76489

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值