c语言判断圆与矩形相交,判断平面内矩形是否与圆相交

该博客介绍了如何在二维平面上判断一个圆是否与矩形相交。提供了四个函数:bPointInCircle用于判断点是否在圆内,bPointInRectangle用于判断点是否在矩形内,bCircleIntersectRectangle用于判断圆是否与矩形相交。通过复数运算简化了坐标计算,并给出了多个示例进行验证。
摘要由CSDN通过智能技术生成

#! /usr/bin/env python

# -*- coding: utf-8 -*-

'''

@filename : 平面坐标内矩形与圆是否相交

@usage    :

@authors  : U{peterguo}

@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) 

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值