Skip to content

Commit

Permalink
is_within helper for Area
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Aug 26, 2024
1 parent ada15f4 commit 2efda00
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions area.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ func (a *Area) Corners() []*Pos {
{a.Pos2.X(), a.Pos2.Y(), a.Pos1.Z()},
}
}

func (a *Area) Intersects(a2 *Area) bool {
for _, p1 := range a.Corners() {
if p1.IsWithin(a2.Pos1, a2.Pos2) {
return true
}
}
for _, p2 := range a2.Corners() {
if p2.IsWithin(a.Pos1, a.Pos2) {
return true
}
}
return false
}
8 changes: 8 additions & 0 deletions area_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ func TestArea(t *testing.T) {
assert.False(t, visited[p.String()])
visited[p.String()] = true
}

a2 := types.NewArea(types.NewPos(0, 0, 0), types.NewPos(1, 2, 3))
assert.True(t, a2.Intersects(a))
assert.True(t, a.Intersects(a2))

a2 = types.NewArea(types.NewPos(0, 0, 0), types.NewPos(1, 1, 1))
assert.False(t, a2.Intersects(a))
assert.False(t, a.Intersects(a2))
}

0 comments on commit 2efda00

Please sign in to comment.