From 706becb764d577a4264071fe1eaf23009bdccfbc Mon Sep 17 00:00:00 2001 From: Tskken Date: Tue, 5 Nov 2019 16:14:36 -0700 Subject: [PATCH 1/3] Update adding new Rect.Intersects Method This addes a new Rect.Intersects method which is around 5x faster then Rect.Intersect when used for basic collision checks. --- geometry.go | 11 +++++++++++ geometry_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/geometry.go b/geometry.go index f3709c2..f91fbd7 100644 --- a/geometry.go +++ b/geometry.go @@ -622,6 +622,17 @@ func (r Rect) Intersect(s Rect) Rect { return t } +// Intersects returns whether or not the given Rect intersects at any point with this Rect. +// +// This function is overall about 5x faster then Intersect, so it is better +// to use if you have no need for the returned Rect from Intersect. +func (r Rect) Intersects(s Rect) bool { + return !(s.Max.X < r.Min.X || + s.Min.X > r.Max.X || + s.Max.Y < r.Min.Y || + s.Min.Y > r.Max.Y) +} + // IntersectCircle returns a minimal required Vector, such that moving the circle by that vector would stop the Circle // and the Rect intersecting. This function returns a zero-vector if the Circle and Rect do not overlap, and if only // the perimeters touch. diff --git a/geometry_test.go b/geometry_test.go index 32bd9f6..82202c9 100644 --- a/geometry_test.go +++ b/geometry_test.go @@ -1555,3 +1555,29 @@ func TestLine_String(t *testing.T) { }) } } + +func BenchmarkRect_Intersect(b *testing.B) { + root := pixel.R(10, 10, 50, 50) + inter := pixel.R(11, 11, 15, 15) + + for i := 0; i < b.N; i++ { + if root.Intersect(inter) != pixel.ZR { + // do a thing + } + + // do a thing + } +} + +func BenchmarkRect_IsIntersect(b *testing.B) { + root := pixel.R(10, 10, 50, 50) + inter := pixel.R(11, 11, 15, 15) + + for i := 0; i < b.N; i++ { + if root.IsIntersect(inter) { + // do a thing + } + + // do a thing + } +} From 837a4efa806866ade0a0d20766d5b290aa4932a0 Mon Sep 17 00:00:00 2001 From: Tskken Date: Tue, 5 Nov 2019 16:16:29 -0700 Subject: [PATCH 2/3] Fixed benchmark error Fixed error for forgeting to change benchark function from IsIntersect to Intersects. --- geometry_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geometry_test.go b/geometry_test.go index 82202c9..e3af737 100644 --- a/geometry_test.go +++ b/geometry_test.go @@ -1574,7 +1574,7 @@ func BenchmarkRect_IsIntersect(b *testing.B) { inter := pixel.R(11, 11, 15, 15) for i := 0; i < b.N; i++ { - if root.IsIntersect(inter) { + if root.Intersects(inter) { // do a thing } From 8d0c306de91bbeab0c12e43c965733d260bfcfd2 Mon Sep 17 00:00:00 2001 From: Tskken Date: Tue, 5 Nov 2019 16:32:55 -0700 Subject: [PATCH 3/3] typo fix fixed typo in comments. --- geometry.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geometry.go b/geometry.go index f91fbd7..c2bbe6e 100644 --- a/geometry.go +++ b/geometry.go @@ -624,7 +624,7 @@ func (r Rect) Intersect(s Rect) Rect { // Intersects returns whether or not the given Rect intersects at any point with this Rect. // -// This function is overall about 5x faster then Intersect, so it is better +// This function is overall about 5x faster than Intersect, so it is better // to use if you have no need for the returned Rect from Intersect. func (r Rect) Intersects(s Rect) bool { return !(s.Max.X < r.Min.X ||