From 793eda81aeb6ee87ac60ba84328f36f3dabfb272 Mon Sep 17 00:00:00 2001 From: kuznetsovin Date: Tue, 11 Feb 2020 18:30:05 +0300 Subject: [PATCH 1/3] add function for create rect from points --- geom.go | 14 ++++++++++++++ geom_test.go | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/geom.go b/geom.go index bee6b54..533a80e 100644 --- a/geom.go +++ b/geom.go @@ -182,6 +182,20 @@ func NewRect(p Point, lengths []float64) (r *Rect, err error) { return } +// NewRectFromPoints constructs and returns a pointer to a Rect given a corner points. +func NewRectFromPoints(minPoint, maxPoint Point) (r *Rect, err error) { + if len(minPoint) != len(maxPoint) { + err = &DimError{len(minPoint), len(maxPoint)} + return + } + + r = new(Rect) + r.p = minPoint + r.q = maxPoint + + return +} + // Size computes the measure of a rectangle (the product of its side lengths). func (r *Rect) Size() float64 { size := 1.0 diff --git a/geom_test.go b/geom_test.go index 015ea13..3d60aa9 100644 --- a/geom_test.go +++ b/geom_test.go @@ -37,6 +37,22 @@ func TestNewRect(t *testing.T) { } } +func TestNewRectFromPoints(t *testing.T) { + p := Point{1.0, -2.5, 3.0} + q := Point{3.5, 5.5, 4.5} + + rect, err := NewRectFromPoints(p, q) + if err != nil { + t.Errorf("Error on NewRect(%v, %v): %v", p, q, err) + } + if d := p.dist(rect.p); d > EPS { + t.Errorf("Expected p == rect.p") + } + if d := q.dist(rect.q); d > EPS { + t.Errorf("Expected q == rect.q") + } +} + func TestNewRectDimMismatch(t *testing.T) { p := Point{-7.0, 10.0} lengths := []float64{2.5, 8.0, 1.5} From 3c8f4d0bad70a92295e37a8f9e0ec7f4c0ec3d79 Mon Sep 17 00:00:00 2001 From: kuznetsovin Date: Thu, 13 Feb 2020 10:31:26 +0300 Subject: [PATCH 2/3] fix code style --- geom.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/geom.go b/geom.go index 533a80e..1d80cc9 100644 --- a/geom.go +++ b/geom.go @@ -189,10 +189,7 @@ func NewRectFromPoints(minPoint, maxPoint Point) (r *Rect, err error) { return } - r = new(Rect) - r.p = minPoint - r.q = maxPoint - + r = &Rect{p: minPoint, q: maxPoint} return } From acbd26355c6ed5210ce094f0ed59eeb6f1169f0e Mon Sep 17 00:00:00 2001 From: kuznetsovin Date: Thu, 13 Feb 2020 10:45:13 +0300 Subject: [PATCH 3/3] add process swap points --- geom.go | 8 ++++++++ geom_test.go | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/geom.go b/geom.go index 1d80cc9..06e83a3 100644 --- a/geom.go +++ b/geom.go @@ -189,6 +189,14 @@ func NewRectFromPoints(minPoint, maxPoint Point) (r *Rect, err error) { return } + //checking that min and max points is swapping + for i, p := range minPoint { + if minPoint[i] > maxPoint[i] { + minPoint[i] = maxPoint[i] + maxPoint[i] = p + } + } + r = &Rect{p: minPoint, q: maxPoint} return } diff --git a/geom_test.go b/geom_test.go index 3d60aa9..d43dd5f 100644 --- a/geom_test.go +++ b/geom_test.go @@ -53,6 +53,24 @@ func TestNewRectFromPoints(t *testing.T) { } } +func TestNewRectFromPointsWithSwapPoints(t *testing.T) { + p := Point{1.0, -2.5, 3.0} + q := Point{3.5, 5.5, 4.5} + + rect, err := NewRectFromPoints(q, p) + if err != nil { + t.Errorf("Error on NewRect(%v, %v): %v", p, q, err) + } + + // we must swap p and q because in function it was swapped + if d := p.dist(rect.q); d > EPS { + t.Errorf("Expected p == rect.p") + } + if d := q.dist(rect.p); d > EPS { + t.Errorf("Expected q == rect.q") + } +} + func TestNewRectDimMismatch(t *testing.T) { p := Point{-7.0, 10.0} lengths := []float64{2.5, 8.0, 1.5}