python question! How to check the circle is in the box, and return true or false
ID: 3781692 • Letter: P
Question
python question!
How to check the circle is in the box, and return true or false,
show code and steps!
A method named insideBox that takes the following inputs: the x coordinate of
the left side of a box (x0), the y coordinate of the top of a box (y0), the width (w)
of the box and the height (h) of the box. The box, the circle and the associated
coordinate system are shown in Figure 1. This method should return true if the
circle is inside the box and false if it is not.
def insideBox(r,x,y,w,h):
??
(0,0) (o, yo) (x,y)Explanation / Answer
import math
def insideBox(x, y, r, w, h,x0, y0):
# boundbox of the Box
y0=y0+h
rright, rbottom = x0 + w/2, y0 + h/2
# bounding box of the circle
cleft, ctop = x-r, y-r
cright, cbottom = x+r, y+r
# if do not intersect
if rright < cleft or x0 > cright or rbottom < ctop or y0 > cbottom:
return True # no collision possible
# check any point of Box is inside circle
for x in (x0, x0+w):
for y in (y0, y0+h):
# compare distance between circle's center(x,y) point and each point of
# the Box with the circle radius r
if math.hypot(x-x, y-y) <= r:
return False # collision
if x0 <= x <= rright and y0 <= y <= rbottom:
return False
return True # no collision
#Circle inside with radius 1
print insideBox(4,4,1,4,4,2,2)
#Circle outside with radius 3
print insideBox(4,4,3,4,4,2,2)
==================================================
akshay@akshay-Inspiron-3537:~/Chegg$ python circauq.py
True
False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.