Nicolas Repiquet
Nicolas Repiquet

Reputation: 9265

Bounds intersection

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:

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 !

Upvotes: 1

Views: 1158

Answers (1)

Beta
Beta

Reputation: 99124

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.)

Upvotes: 3

Related Questions