python定义一个圆_Python-矩形和圆形
#######15.9 p175
import math
class Circle: #
"""
This is a circle Class,
incude Center and radius
"""
class Point:
"""
This is a point
"""
class Rectangle():
"""
This is a 矩形
"""
radius=Circle()
radius.x=75.0
center=Circle()
center.box=Point()
center.box.x=150.0
center.box.y=100.0
##矩形
box=Rectangle()
box.wei=100.0
box.hei=100.0
box.point=Point()
#計算點到圓心的距離
def point_in_len(x,y):
b=math.sqrt((x-center.box.x)**2+(y-center.box.y)**2)
return b
#計算點是否在圓內(nèi)
def point_in_circle(x,y):
b=point_in_len(x,y)
if b <=radius.x:
print( b)
return True
else:
return False
#
#計算矩形全部在圓內(nèi)
def rect_in_circle(x,y):
x1=x+box.wei
y1=y+box.hei
b1=math.sqrt((x-center.box.x)**2+(y-center.box.y)**2)
b2= math.sqrt((x1-center.box.x)**2+(y-center.box.y)**2)
b3= math.sqrt((x-center.box.x)**2+(y1-center.box.y)**2)
b4=math.sqrt(box.wei**2+box.hei**2)
if b4 <=radius.x:
if b1<=radius.x and b2<=radius.x and b3<=radius.x:
return True
else:
return False
else:
return False
def rect_circle_overlap(x,y): ###
#算如果矩形在圓形的一部分就返回Ture,
#原理:比對在矩形的范圍內(nèi)所有的點,是否有到圓心的距離,小于半徑的
###原理:如果有符合的就計數(shù)+1
##并計算 計數(shù)是否大于1,如果大于1,則認(rèn)為矩形有點是在圓形中
x2=int(x)
x3=x2+int(x+box.wei)
y2=int(y)
y3=y2+int(y+box.hei)
count=0
for x1 in range(x2,x3):
for y1 in range(y2,y3):
len1=math.sqrt((x1-center.box.x)**2+(y1-center.box.y)**2)
if len1 >radius.x:
count=count+0
else:
count+=1
if count >=1:
return True
point=Point()
point.x=90.0
point.y=90.0
print(point_in_circle(point.x,point.y))
###矩形
box.point.x=160.0
box.point.y=100.0
###計算矩形是否全部包含在圓形內(nèi)
print(rect_in_circle(box.point.x,box.point.y))
##計算矩形是否有部分在圓形內(nèi)
print(rect_circle_overlap(box.point.x,box.point.y))
總結(jié)
以上是生活随笔為你收集整理的python定义一个圆_Python-矩形和圆形的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: navicat使用查询向表中插入一行记录
- 下一篇: python函数的命名_18:命名, 变