← Back to challenges

Rectangle Collision Detection

PythonHardmathclasses

Instructions

Make a Rectangle class with four parameters, an x, a y (representing the top-left corner of the rectangle), a width and a height exclusively in that order.

Lastly, make a function intersecting that takes two Rectangle objects and returns True if those objects are intersecting (colliding), else return False.

IMPORTANT: horizontal axis goes from left to right while vertical axis goes from top to bottom.

Examples

a = Rectangle(10, 20, 100, 20)
b = Rectangle(10, 40, 15, 20)
c = Rectangle(50, 50, 20, 30)

intersecting(a, b) ➞ True

intersecting(a, c) ➞ False

intersecting(b, c) ➞ False

Notes

  • No negative values will be passed.
  • If it's a bit confusing, try imagining the rectangle with the given width and height and then place their top-left corner to the given x, y coordinates.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.