Skip to content

Commit

Permalink
Fix vec2 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlewis committed Aug 14, 2021
1 parent ab8f9d3 commit 1644591
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module github.com/gmlewis/go3d

go 1.16

2 changes: 1 addition & 1 deletion vec2/rect.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (rect *Rect) Contains(other *Rect) bool {
func (rect *Rect) Intersects(other *Rect) bool {
return other.Max[0] >= rect.Min[0] &&
other.Min[0] <= rect.Max[0] &&
other.Max[1] >= rect.Min[0] &&
other.Max[1] >= rect.Min[1] &&
other.Min[1] <= rect.Max[1]
}

Expand Down
150 changes: 150 additions & 0 deletions vec2/rect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package vec2

import "testing"

func TestIntersects(t *testing.T) {
tests := []struct {
name string
a *Rect
b *Rect
want bool
}{
{
name: "a touches top left of b",
a: &Rect{Min: T{0, 0}, Max: T{1, 1}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
{
name: "a touches top middle of b",
a: &Rect{Min: T{1, 0}, Max: T{2, 1}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
{
name: "a touches top right of b",
a: &Rect{Min: T{2, 0}, Max: T{3, 1}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
{
name: "a touches center left of b",
a: &Rect{Min: T{0, 1}, Max: T{1, 2}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
{
name: "a on top of b",
a: &Rect{Min: T{1, 1}, Max: T{2, 2}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
{
name: "a touches center right of b",
a: &Rect{Min: T{2, 1}, Max: T{3, 2}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
{
name: "a touches bottom left of b",
a: &Rect{Min: T{0, 2}, Max: T{1, 3}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
{
name: "a touches bottom middle of b",
a: &Rect{Min: T{1, 2}, Max: T{2, 3}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
{
name: "a touches bottom right of b",
a: &Rect{Min: T{2, 2}, Max: T{3, 3}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: true,
},
// does not touch
{
name: "a does not touch top left of b",
a: &Rect{Min: T{0, -1}, Max: T{1, 0}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch top left of b - version 2",
a: &Rect{Min: T{-1, 0}, Max: T{0, 1}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch top middle of b",
a: &Rect{Min: T{1, -1}, Max: T{2, 0}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch top right of b",
a: &Rect{Min: T{2, -1}, Max: T{3, 0}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch top right of b - version 2",
a: &Rect{Min: T{3, 0}, Max: T{4, 1}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch center left of b",
a: &Rect{Min: T{-1, 1}, Max: T{0, 2}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch center right of b",
a: &Rect{Min: T{3, 1}, Max: T{4, 2}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch bottom left of b",
a: &Rect{Min: T{0, 3}, Max: T{1, 4}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch bottom left of b - version 2",
a: &Rect{Min: T{-1, 2}, Max: T{0, 3}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch bottom middle of b",
a: &Rect{Min: T{1, 3}, Max: T{2, 4}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch bottom right of b",
a: &Rect{Min: T{2, 3}, Max: T{3, 4}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
{
name: "a does not touch bottom right of b - version 2",
a: &Rect{Min: T{3, 2}, Max: T{4, 3}},
b: &Rect{Min: T{1, 1}, Max: T{2, 2}},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.a.Intersects(tt.b)

if got != tt.want {
t.Errorf("got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1644591

Please sign in to comment.