Question

In: Computer Science

Python: A rectangle is axis-aligned if its sides are parallel to the coordinate axes. An axis-aligned...

Python: 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.
Context. Detection of the overlap of bounding boxes is used in collision detection (e.g., robotics, games). A bounding box of an object is a rectangle (2D) or box (higher dimensions) that contains the object. Since two objects can collide only if their bounding boxes overlap, collision detection of bounding boxes is used as an efficient test, with the more expensive intersection of the objects used only in the rare cases when the bounding boxes overlap. Collision detection is used during motion planning of a robot, or interactively during a game to prevent, say, running through walls. Observation. It would be more elegant to collapse the 8 parameters of this function into two rectangle parameters, but that requires more advanced Python syntax. You may revisit these functions once we have that power.

Solutions

Expert Solution

Code:

def rOverlap(x1,y1,w1,h1,x2,y2,w2,h2):

bottomLeft1 = (x1,y1)
bottomRight1 = (x1 + w1,y1)
topLeft1 = (x1,y1 + h1)
topRight1 = (x1 + w1,y1 + h1)

bottomLeft2 = (x2,y2)
bottomRight2 = (x2 + w2,y2)
topLeft2 = (x2,y2 + h2)
topRight2 = (x2 + w2,y2 + h2)

rect1 = [bottomLeft1,bottomRight1,topLeft1,topRight1]
rect2 = [bottomLeft2,bottomRight2,topLeft2,topRight2]

#If any two co-ordinates match, then return true
for i in rect1:
for j in rect2:
if i == j:
return True

#If one rectangle is to the side of other

if bottomRight2[0] < topLeft1[0] or topLeft2[0] > bottomRight1[0]:
return False

#If one rectangle is on the top of other

if bottomRight2[1] > topLeft1[1] or topLeft2[1] < bottomRight1[1]:
return False

#If both the conditions fail
return True

#Test
x1 = 0
y1 = 0
h1 = 10
w1 = 10

x2 = 5
y2 = 5
h2 = 3
w2 = 3

print(rOverlap(x1,y1,w1,h1,x2,y2,w2,h2))
  
The output of the above code is True for the given test case.

Please appreciate the solution if you find it helpful.

If you have any doubts in the solution, feel free to ask me in the comment section.


Related Solutions

Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes.
 c programming Objectives:Learn about conditional statements and Boolean operators.Problem:Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.Example:1) Suppose R is the rectangle centered at the origin height and width equal to one and S is the rectangle centered at the point (1,1) with a height equal to 2 and width equal to 3. Then...
Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.
 C LANGUAGEObjectives:Learn about conditional statements and Boolean operators.Problem:Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.Example:1) Suppose R is the rectangle centered at the origin height and width equal to one and S is the rectangle centered at the point (1,1) with a height equal to 2 and width equal to 3. Then...
Consider a rectangle in the first quadrant with two sides along the axes (so that one...
Consider a rectangle in the first quadrant with two sides along the axes (so that one corner is at (0, 0)). Find the dimensions of the largest such rectangle that is inscribed in the region in the first quadrant below the curve y = (x − 2)^2 . (Draw a picture)
Three infinite straight wires are fixed in place and aligned parallel to the z-axis as shown....
Three infinite straight wires are fixed in place and aligned parallel to the z-axis as shown. The wire at (x,y) = (-13.5 cm, 0) carries current I1 = 3.2 A in the negative z-direction. The wire at (x,y) = (13.5 cm, 0) carries current I2 = 1.4 A in the positive z-direction. The wire at (x,y) = (0, 23.4 cm) carries current I3 = 5.1 A in the positive z-direction. 1) What is Bx(0,0), the x-component of the magnetic field...
4. Define the width of a rectangle as the longest length of its sides. Given a...
4. Define the width of a rectangle as the longest length of its sides. Given a closed rectangle A in Rn and a partition P of A, define the mesh of P as the maximum width of its subrectangles. Prove that a bounded function f : A → R is integrable on A if and only if, for every > 0, there is a δ > 0 such that U(f, P) − L(f, P) < for every partition P of...
Write a program that asks the user for the lengths of the sides of a rectangle....
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem). This question should be...
Consider a region R bound by the coordinate axes and y = ( 9 + x...
Consider a region R bound by the coordinate axes and y = ( 9 + x 2 ) − 1 2 on 0 ≤ x ≤ 4. a. Find the area of R. b. Suppose R is revolved about the x-axis to form a solid. Find the volume of the solid. c. Suppose R is revolved about the y-axis to form a solid. Find the volume of the solid.
Draw ? on a torus (or the schematic representation where opposite sides of a rectangle are...
Draw ? on a torus (or the schematic representation where opposite sides of a rectangle are identified.)
A 60º quartz prism is cut with its faces parallel to the optic axis. The refractive...
A 60º quartz prism is cut with its faces parallel to the optic axis. The refractive indices for ordinary and extraordinary rays are 1.5422 and 1.5533 respectively. Assume that the wavelength of the light used is 5890 Å. Calculate the angle of minimum deviation for yellow light for ordinary and extraordinary rays.
The parallel axis theorem relates Icm, the moment of inertia of an object about an axis passing through its center of m...
The parallel axis theorem relates Icm, the moment of inertia of an object about an axis passing through its center of mass, to Ip, the moment of inertia of the same object about a parallel axis passing through point p. The mathematical statement of the theorem is Ip=Icm+Md2, where d is the perpendicular distance from the center of mass to the axis that passes through point p, and M is the mass of the object. Part A Suppose a uniform slender rod...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT