1

I have 2 rectangles, one which is basically the other scaled up, like so enter image description here

I'd like to see if a set of xy coords falls within the XOR difference, ie

enter image description here

What's the best math to do this?

Difference (XOR) between two rectangles, as rectangles? does kind of what I want, but not exactly, and it seems somewhat... inelegant.

Alternately, I could work with something that returns "true" if x/y coords are within 10% of the edge of the outer rectangle

Community
  • 1
  • 1
jamesson
  • 317
  • 3
  • 18

2 Answers2

2
bool isInIntersection(pt, rect1, rect2)
{
    return isInRect(pt, rect1) && !isInRect(pt, rect2);
}

bool isInRect(pt, rect)
{
    return (pt.x >= rect.x1) && (pt.x < rect.x2)
        && (pt.y >= rect.y1) && (pt.y < rect.y2);
}

where I'm assuming rect1 is the outer rectangle.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

Heres an example in javascript for finding if a point is in a rectangle. Point-in-rectangle testing Then its just a matter if testing if its in Rectangle1 and if so, if its not in Rectangle2.

Community
  • 1
  • 1
Kratz
  • 4,280
  • 3
  • 32
  • 55