Omegastick
Omegastick

Reputation: 1911

Check if a rectangle bisects another rectangle

I have two rectangles on a grid, defined by x, y, width, and height (all integers).

I want to tell if one of them bisects the other. That is, not just overlaps, but goes all the way through so as to create three rectangles.

Example: Overlap vsbisect

Is there a relatively time-efficient algorithm to do this?

Upvotes: 0

Views: 61

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32667

If we look at the second example in your question, we can find the following conditions to determine if rectangle A bisects rectangle B vertically:

xA <= xB && xA + widthA >= xB + widthB && yA > yB && yA + heightA < yB + heightB

Similarly, there are three more cases (horizontal and the same for the other way around). If any of these cases apply, you have a bisection.

Upvotes: 2

Related Questions