Question

In Python rOverlap (x1, y1, w1, h1, x2, y2, w2, h2) A rectangle is axis-aligned if...

In Python rOverlap (x1, y1, w1, h1, x2, y2, w2, h2) A rectangle is axis-aligned if its sides are parallel to the coordinate axes. An axis-aligned rectangle will be defined by its bottom left corner (x,y), its (nonnegative) width w, and its (nonnegative) height h. The Cartesian coordinates (x,y) behave in the normal mathematical way: increasing x moves right, increasing y moves up. (In future, we will see situations where different conventions are used.) Write the function rOverlap that tests whether 2 axis-aligned rectangles overlap. rOverlap takes 8 floats (x1,y1,w1,y1 represent the first rectangle and x2,y2,w2,y2 represent the second rectangle) and returns True if the two rectangles overlap/touch, even if only at a single point, and False otherwise.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If we know the top-right and the bottom-left coordinates of both the triangle then we can tell if they overlap or not:

The two triangles cannot overlap in the following four cases:

  1. when rectangle 1 is entire on left of rectangle 2
  2. when rectangle 1 is entire on right of rectangle 2
  3. when rectangle 1 is entire on top of rectangle 2
  4. when rectangle 1 is entire on bottom of rectangle 2

Condition for left: The rectangle1 will be on left of rectangle 2 if the top right x-coordinate of the rectangle 1 will be less than bottom left x-coordinate of rectangle 2

Condition for right: The rectangle1 will be on right of rectangle 2 if the top right x-coordinate of the rectangle 2 will be less than bottom left x-coordinate of rectangle 1

Condition for top: The rectangle 1 will be on top of rectangle 2 if the top right y-coordinate of the rectangle 2 will be less than bottom left y-coordinate of rectangle 1

Condition for bottom: The rectangle1 will be on bottom of rectangle 2 if the top right y-coordinate of the rectangle 1 will be less than bottom left y-coordinate of rectangle 2

Here is the code to do so:


Here is the sample output:

and here is another sample output:

The codes are well commented and easy to understand, if the answer helped you please upvote and if you have any doubts please comment i will surely help. please take care of the indentation while copying the code. Check from the screenshots provided.

Here is the code:

def rOverlap(x1rec1, y1rec1, w1, h1, x1rec2, y1rec2, w2, h2):
# get the top right corner cordinates for the two rectangles
x2rec1 = x1rec1 + w1
y2rec1 = y1rec1 + h1
x2rec2 = x1rec2 + w2
y2rec2 = y1rec2 + h2
  
if (x2rec1 <= x1rec2 or # left
y2rec1 <= y1rec2 or # bottom
x1rec1 >= x2rec2 or # right
y1rec1 >= y2rec2): # top
return True
else:
return False
# Take the input and run a sample test to check whether they overlap or not
x1rec1 = float(input("Enter the left-most x-coordinate of first rectangle: "))
y1rec1 = float(input("Enter the left-most x-coordinate of first rectangle: "))
w1= float(input("Enter the width of first rectangle: "))
h1= float(input("Enter the height of first rectangle: "))

x1rec2 = float(input("Enter the left-most x-coordinate of second rectangle: "))
y1rec2 = float(input("Enter the left-most y-coordinate of second rectangle: "))
w2= float(input("Enter the width of second rectangle: "))
h2= float(input("Enter the height of second rectangle: "))

if rOverlap(x1rec1, y1rec1, w1, h1, x1rec2, y1rec2, w2, h2):
print("The two triangles dont overlap")
else:
print("the two triangles overlap")

Add a comment
Know the answer?
Add Answer to:
In Python rOverlap (x1, y1, w1, h1, x2, y2, w2, h2) A rectangle is axis-aligned if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. In IntelliJ create a new project called F1_2 2. In the Project window create a...

    1. In IntelliJ create a new project called F1_2 2. In the Project window create a new Java package called F1_2. This can be done by right clicking on src and going to New ! Package. 3. In package lab1 2 create a class called Rectangle. This can be done by right clicking on the package and going to New ! Java Class 4. At the beginning of Rectangle.java add the line package lab1 2; 5. In Rectangle.java create a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT