1

Given a Bounds structure like this:

struct Bounds {
  public double xMin;
  public double xMax;
  public double yMin;
  public double yMax;
}

I'm trying to find out how two Bounds A and B intersect. Possible results are:

  • A and B do not intersect at all
  • A and B are equal
  • A fully contains B
  • B fully contains A
  • A and B intersect each other

My first and naive attempt at it, is to test how many points of A are in B and how many points of B are in A, but I need this test to be as fast as possible and there is probably a better way to do it.

Thanks a lot !

Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53

1 Answers1

3

Try it in 2D 1D first. It should be clear how to test two [xmin, xmax] objects for those five possible results. Then do the same for [ymin, ymax]. Then combine the two results:

  • (no intersection)x + (anything)y = (no intersection)
  • (equal)x + (something)y = (something)
  • (A contains B)x + (A contains B)y = (A contains B)
  • (A contains B)x + (A and B intersect)y = (A and B intersect)
  • (A contains B)x + (B contains A)y = (A and B intersect)

(I think that covers it.)

Beta
  • 96,650
  • 16
  • 149
  • 150