forked from AllenDowney/ThinkPython2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircle.py
124 lines (89 loc) · 2.42 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
import copy
from Point1 import Point, Rectangle, print_point
from Point1_soln import distance_between_points
class Circle:
"""Represents a circle.
Attributes: center, radius
"""
def point_in_circle(point, circle):
"""Checks whether a point lies inside a circle (or on the boundary).
point: Point object
circle: Circle object
"""
d = distance_between_points(point, circle.center)
print(d)
return d <= circle.radius
def rect_in_circle(rect, circle):
"""Checks whether the corners of a rect fall in/on a circle.
rect: Rectangle object
circle: Circle object
"""
p = copy.copy(rect.corner)
print_point(p)
if not point_in_circle(p, circle):
return False
p.x += rect.width
print_point(p)
if not point_in_circle(p, circle):
return False
p.y -= rect.height
print_point(p)
if not point_in_circle(p, circle):
return False
p.x -= rect.width
print_point(p)
if not point_in_circle(p, circle):
return False
return True
def rect_circle_overlap(rect, circle):
"""Checks whether any corners of a rect fall in/on a circle.
rect: Rectangle object
circle: Circle object
"""
p = copy.copy(rect.corner)
print_point(p)
if point_in_circle(p, circle):
return True
p.x += rect.width
print_point(p)
if point_in_circle(p, circle):
return True
p.y -= rect.height
print_point(p)
if point_in_circle(p, circle):
return True
p.x -= rect.width
print_point(p)
if point_in_circle(p, circle):
return True
return False
def main():
box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 50.0
box.corner.y = 50.0
print(box.corner.x)
print(box.corner.y)
circle = Circle
circle.center = Point()
circle.center.x = 150.0
circle.center.y = 100.0
circle.radius = 75.0
print(circle.center.x)
print(circle.center.y)
print(circle.radius)
print(point_in_circle(box.corner, circle))
print(rect_in_circle(box, circle))
print(rect_circle_overlap(box, circle))
if __name__ == '__main__':
main()