Skip to content

Commit

Permalink
Add Intersects()
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed May 17, 2019
1 parent 33d1050 commit 1786e27
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions xmath/geom/rect.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ func (r *Rect) IsEmpty() bool {
return r.Width <= 0 || r.Height <= 0
}

// Intersects returns true if this rect and the other rect intersect.
func (r *Rect) Intersects(other Rect) bool {
if !r.IsEmpty() && !other.IsEmpty() {
return math.Min(r.X+r.Width, other.X+other.Width)-math.Max(r.X, other.X) > 0 &&
math.Min(r.Y+r.Height, other.Y+other.Height)-math.Max(r.Y, other.Y) > 0
}
return false
}

// Intersect this Rect with another Rect, storing the result into this Rect.
func (r *Rect) Intersect(other Rect) {
if r.IsEmpty() || other.IsEmpty() {
Expand Down

0 comments on commit 1786e27

Please sign in to comment.