Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 4 - Selections - Programming Exercises - Page 127: 4.28

Answer

code

Work Step by Step

# 4.28 (Geometry: two rectangles) Write a program that prompts the user to enter the # center x-, y-coordinates, width, and height of two rectangles and determines # whether the second rectangle is inside the first or overlaps with the first, as shown # in Figure 4.10. Test your program to cover all cases. x1, y1, w1, h1 = eval(input("Enter r1's center x-, y-coordinates, width, and height: ")) x2, y2, w2, h2 = eval(input("Enter r2's center x-, y-coordinates, width, and height: ")) xDistance = x1 - x2 if x1 - x2 >= 0 else x2 - x1 yDistance = y1 - y2 if y1 - y2 >= 0 else y2 - y1 if xDistance <= (w1 - w2) / 2 and yDistance <= (h1 - h2) / 2: print("r2 is inside r1") elif xDistance <= (w1 + w2) / 2 and yDistance <= (h1 + h2) / 2: print("r2 overlaps r1") else: print("r2 does not overlap r1")
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.