-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathCircle.Py
55 lines (44 loc) · 1.13 KB
/
Circle.Py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'''To check if a point or rectangle lies in/on circle'''
import math
class Point:
'''Represents a point in 2D
Atrributes: x,y'''
class Rectangle:
'''Represents a rectangle.
Attributes: C1 and C2 of primary diagonal'''
class Circle:
'''Represents a Circle.
Attributes: Center and radius'''
def distance(p1,p2):
res=math.sqrt((p2.x-p1.x)**2 + (p2.y-p1.y)**2)
return res
def point_in_circle(p,c):
'''p:point , c:circle'''
if distance(p,c.center)<=c.radius:
return True
else:
return False
def rect_in_circle(rect,c):
if distance(rect.c1,c.center)>c.radius:
return False
if distance(rect.c2,c.center)>c.radius:
return False
return True
def main():
rect=Rectangle()
rect.c1=Point()
rect.c2=Point()
rect.c1.x=-3
rect.c1.y=1
rect.c2.x=3
rect.c2.y=-1
circle=Circle()
circle.center=Point()
circle.center.x=0
circle.center.y=0
circle.radius=5
print(point_in_circle(rect.c1,circle))
print(point_in_circle(rect.c2,circle))
print(rect_in_circle(rect,circle))
if __name__=='__main__':
main()