From 46e79f21b994aa6126161cbbb0b22fcd0e6bef7a Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 5 Jul 2017 19:35:11 +0200 Subject: [PATCH 1/4] add Rect.Area --- geometry.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/geometry.go b/geometry.go index 503d8cb..56283a9 100644 --- a/geometry.go +++ b/geometry.go @@ -203,6 +203,11 @@ func (r Rect) Size() Vec { return V(r.W(), r.H()) } +// Area returns the area of r. If r is not normalized, area may be negative. +func (r Rect) Area() float64 { + return r.W() * r.H() +} + // Center returns the position of the center of the Rect. func (r Rect) Center() Vec { return Lerp(r.Min, r.Max, 0.5) From bee65f5833ae00d6c7b0f8ec2ef5cbaf275aa927 Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 5 Jul 2017 19:51:54 +0200 Subject: [PATCH 2/4] add Rect.Intersect --- geometry.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/geometry.go b/geometry.go index 56283a9..1c61477 100644 --- a/geometry.go +++ b/geometry.go @@ -160,8 +160,8 @@ type Rect struct { // Note that the returned rectangle is not automatically normalized. func R(minX, minY, maxX, maxY float64) Rect { return Rect{ - Min: V(minX, minY), - Max: V(maxX, maxY), + Min: Vec{minX, minY}, + Max: Vec{maxX, maxY}, } } @@ -257,7 +257,7 @@ func (r Rect) Contains(u Vec) bool { return r.Min.X <= u.X && u.X <= r.Max.X && r.Min.Y <= u.Y && u.Y <= r.Max.Y } -// Union returns a minimal Rect which covers both r and s. Rects r and s should be normalized. +// Union returns the minimal Rect which covers both r and s. Rects r and s must be normalized. func (r Rect) Union(s Rect) Rect { return R( math.Min(r.Min.X, s.Min.X), @@ -267,6 +267,20 @@ func (r Rect) Union(s Rect) Rect { ) } +// Intersect returns the maximal Rect which is covered by both r and s. Rects r and s must be normalized. +func (r Rect) Intersect(s Rect) Rect { + t := R( + math.Min(r.Max.X, s.Max.X), + math.Min(r.Max.Y, s.Max.Y), + math.Max(r.Min.X, s.Min.X), + math.Max(r.Min.Y, s.Min.Y), + ) + if t.Min.X >= t.Max.X || t.Min.Y >= t.Max.Y { + return Rect{} + } + return t +} + // Matrix is a 3x2 affine matrix that can be used for all kinds of spatial transforms, such // as movement, scaling and rotations. // From 4137f87f2206542515f6165714d785d21f865a5d Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 5 Jul 2017 19:58:09 +0200 Subject: [PATCH 3/4] clarify Rect.Intersect doc --- geometry.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/geometry.go b/geometry.go index 1c61477..3b2b8b3 100644 --- a/geometry.go +++ b/geometry.go @@ -268,6 +268,8 @@ func (r Rect) Union(s Rect) Rect { } // Intersect returns the maximal Rect which is covered by both r and s. Rects r and s must be normalized. +// +// If r and s don't overlap, this function returns R(0, 0, 0, 0). func (r Rect) Intersect(s Rect) Rect { t := R( math.Min(r.Max.X, s.Max.X), From d7487f1f7abdc3e10098cb67cf458ca3fbb5acfd Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 5 Jul 2017 19:51:54 +0200 Subject: [PATCH 4/4] fix Rect.Intersect --- geometry.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geometry.go b/geometry.go index 3b2b8b3..99514cc 100644 --- a/geometry.go +++ b/geometry.go @@ -272,10 +272,10 @@ func (r Rect) Union(s Rect) Rect { // If r and s don't overlap, this function returns R(0, 0, 0, 0). func (r Rect) Intersect(s Rect) Rect { t := R( - math.Min(r.Max.X, s.Max.X), - math.Min(r.Max.Y, s.Max.Y), math.Max(r.Min.X, s.Min.X), math.Max(r.Min.Y, s.Min.Y), + math.Min(r.Max.X, s.Max.X), + math.Min(r.Max.Y, s.Max.Y), ) if t.Min.X >= t.Max.X || t.Min.Y >= t.Max.Y { return Rect{}