Merge pull request #290 from dusk125/master

Fixing #277: Rect.Intersect and Rect.Intersects now have consistent behavior
This commit is contained in:
Allen Ray 2021-08-18 20:06:47 -04:00 committed by GitHub
commit d940a42454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 23 deletions

View File

@ -222,10 +222,10 @@ func (r Rect) Intersect(s Rect) Rect {
// 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 ||
s.Min.X > r.Max.X ||
s.Max.Y < r.Min.Y ||
s.Min.Y > r.Max.Y)
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 rect by that vector would stop the Circle

View File

@ -329,28 +329,56 @@ func TestRect_IntersectionPoints(t *testing.T) {
}
}
func BenchmarkRect_Intersect(b *testing.B) {
root := pixel.R(10, 10, 50, 50)
inter := pixel.R(11, 11, 15, 15)
var rectIntTests = []struct {
name string
r1, r2 pixel.Rect
want pixel.Rect
intersect bool
}{
{
name: "Nothing touching",
r1: pixel.R(0, 0, 10, 10),
r2: pixel.R(21, 21, 40, 40),
want: pixel.ZR,
},
{
name: "Edge touching",
r1: pixel.R(0, 0, 10, 10),
r2: pixel.R(10, 10, 20, 20),
want: pixel.ZR,
},
{
name: "Bit of overlap",
r1: pixel.R(0, 0, 10, 10),
r2: pixel.R(0, 9, 20, 20),
want: pixel.R(0, 9, 10, 10),
intersect: true,
},
{
name: "Fully overlapped",
r1: pixel.R(0, 0, 10, 10),
r2: pixel.R(0, 0, 10, 10),
want: pixel.R(0, 0, 10, 10),
intersect: true,
},
}
for i := 0; i < b.N; i++ {
if root.Intersect(inter) != pixel.ZR {
// do a thing
func TestRect_Intersect(t *testing.T) {
for _, tt := range rectIntTests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r1.Intersect(tt.r2); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rect.Intersect() = %v, want %v", got, tt.want)
}
// 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.Intersects(inter) {
// do a thing
func TestRect_Intersects(t *testing.T) {
for _, tt := range rectIntTests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r1.Intersects(tt.r2); got != tt.intersect {
t.Errorf("Rect.Intersects() = %v, want %v", got, tt.want)
}
// do a thing
})
}
}