added method to determine distance between points
should be valuable to know what distance between two points is. Also added testing
This commit is contained in:
parent
b5a9726d2d
commit
b9bc23566f
6
point.go
6
point.go
@ -1,5 +1,11 @@
|
|||||||
package rect
|
package rect
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
type Point struct {
|
type Point struct {
|
||||||
X, Y float64
|
X, Y float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func distance(p1, p2 Point) float64 {
|
||||||
|
return math.Sqrt(math.Pow((p1.X-p2.X), 2) + math.Pow((p1.Y-p2.Y), 2))
|
||||||
|
}
|
||||||
|
29
point_test.go
Normal file
29
point_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package rect
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDistance(t *testing.T) {
|
||||||
|
var distanceTest = []struct {
|
||||||
|
p []Point
|
||||||
|
expected float64
|
||||||
|
}{
|
||||||
|
{[]Point{Point{1, 1}, Point{4, 5}}, 5},
|
||||||
|
{[]Point{Point{-1, -1}, Point{2, 3}}, 5},
|
||||||
|
{[]Point{Point{1, 1}, Point{2, 2}}, math.Sqrt(2)},
|
||||||
|
{[]Point{Point{1, 1}, Point{40, 20}}, math.Sqrt(1882)},
|
||||||
|
}
|
||||||
|
for _, rt := range distanceTest {
|
||||||
|
actual := distance(rt.p[0], rt.p[1])
|
||||||
|
if actual != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed spiral:\n\texpected: %d\n\t actual: %d",
|
||||||
|
rt.expected,
|
||||||
|
actual,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user