Write a function, `rec_intersection(rect1, rect2)` and returns the intersection of the two.
Rectangles are represented as a pair of coordinate-pairs: the bottom-left and top-right coordinates (given in `[x, y]` notation).
Hint: You can calculate the left-most x coordinate of the intersection by taking the maximum of the left-most x coordinate of each rectangle. Likewise, you can calculate the top-most y coordinate of the intersection by taking the minimum of the top most y coordinate of each rectangle.
This is probably the hardest problem I’ve encountered do date. It’s a bit more logic than math and I definitely overcomplicated it by trying to make sense of it with math, which is not my strength..
Some helpful things to remember:
- Plot out the coordinates and draw the rectangles on a piece of paper first.
- Try not to confuse the (x,y) coordinates with arrays. (This part tripped me up the most).
- The trick is to use max and min to get the left and right coordinates of the intersection, respectively. (This is a bit counterintuitive).
- To access a value of a multidimensional array, use a[i][j], where i is the index of a, and j is the index of a[i] array.
Anyways, here’s the solution with my annotated comments: