From 2efda003ff425f76fd61b4ba18861ca6a25f556e Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 26 Aug 2024 15:59:55 +0200 Subject: [PATCH] is_within helper for Area --- area.go | 14 ++++++++++++++ area_test.go | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/area.go b/area.go index aedc99f..dcb1d39 100644 --- a/area.go +++ b/area.go @@ -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 +} diff --git a/area_test.go b/area_test.go index cc5e3f4..36174f6 100644 --- a/area_test.go +++ b/area_test.go @@ -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)) }