-3

I have the following situation, for illustrative purposes split into 2 situations.

enter image description here

The green rectangle in the middle is the rectangle that I am referring to as my base, since it is the rectangle that is constant.

Now I'd like to know if a yellow rectangle is intersecting, is inside or fully encapsulates the green rectangle.

I've seen this post and understand it, but (unless I forget something) it ignores the case of a yellow rectangle being inside the green rectangle. The simplest solution I can think of is to double check the 4 points in either order, but is that the only("best") solution?

Meik Vtune
  • 450
  • 8
  • 25
  • Simply iterate over all 4 vertices and check if all of them are inside of the green rectangle. There is no need for some magic. – Amongalen May 07 '19 at 12:14
  • 1
    Let's see. Yellow.left < Green.right? true. Yellow.right > Green.left? true. Yellow.top > Green.bottom? true. Yellow.bottom < Green.top? Also true. So where is the problem? – RealSkeptic May 07 '19 at 12:25
  • Do you need full information or is it just enough to know that the two rectangles have a non-empty intersection ? –  May 07 '19 at 16:08
  • Care to answer my question ? –  May 08 '19 at 06:35

1 Answers1

1

Two rectangles A, B have a non-empty intersection iif

A.l < B.r and A.r > B.l and A.t < B.b and A.b > B.t

(left, right, top, bottom coordinates, y downward).

A wholly contains B iif

A.l <= B.l and A.r >= B.r and A.t <= B.t and A.b >= B.b
  • If `A` is the smaller rectangle, meaning `B` wholly contains `A`, is there a better way to check that then to simply swap `A` and `B` in code? – Meik Vtune May 08 '19 at 06:19
  • @MeikVtune: if you wan to perform all three tests, you can perform a merge of the sequences `A.l`, `A.r` and `B.l`, `B.r`, and similar for the other axis. This will take 6 comparisons and the history of the merging contains all information. If you organize this in a hard-coded decision tree, you can get very efficient code. –  May 08 '19 at 06:38