Skip to content

Commit

Permalink
add process swap points
Browse files Browse the repository at this point in the history
  • Loading branch information
kuznetsovin committed Feb 13, 2020
1 parent 3c8f4d0 commit acbd263
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit acbd263

Please sign in to comment.