In: Computer Science
Write a python function that accepts two integer values corresponding to the point (x, y). Check whether the point is within the rectangle centered at (0, 0) with width 20 and height 15. For example, (-9, 7) is inside the rectangle and (11, 4) is outside the rectangle, as shown in the figure. Return True if the point falls within the rectangle and False otherwise
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
#returns True if x,y is within rectangle
def isInRect(x, y):
#width and height of rectangle
width=20
height=15
#since the rectangle is centered at (0,0),
we just need to check if
# -width/2 <= x <= width/2 and -height/2
<= y <= height/2
if x>=(-width/2)
and x<=(width/2) and
y>=(-height/2) and y<=(height/2):
#inside or in
boundary
return
True
#outside
return False
#testing
print(isInRect(-9, 7)) #True
print(isInRect(11, 4)) #False
#output
True
False