From b5cc7c5aea38a7b91caa3c117de57ea9c652e03b Mon Sep 17 00:00:00 2001 From: Fraser Graham Date: Thu, 8 Aug 2013 21:54:52 -0700 Subject: [PATCH] added some stuff --- govector_test.go | 49 +++++++++++++++++++++++++++++++++++------------- vectorpoint.go | 18 ++++++++++++++++-- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/govector_test.go b/govector_test.go index 654f93c..9937070 100644 --- a/govector_test.go +++ b/govector_test.go @@ -1,29 +1,52 @@ package govector import ( - // "fmt" + "fmt" "testing" ) -func TestSub(t *testing.T) { - p1 := Point2d{10, 10} - p2 := Point2d{5, 5} - - v := p1.sub(p2) - - if v != (Vector2d{5, 5}) { - t.Errorf("Sub Error") - } -} - -func TestMag(t *testing.T) { +func TestSubMag(t *testing.T) { p1 := Point2d{10, 10} p2 := Point2d{7, 6} v := p1.sub(p2) + + if v != (Vector2d{3, 4}) { + t.Errorf("Sub Error") + } + m := v.mag() if m != 5 { t.Errorf("Mag Error") } } + +func TestScale(t *testing.T) { + v := Vector2d{3, 4} + m := v.mag() + + if m != 5 { + t.Errorf("Mag Error") + } + + m2 := v.scale(2) + + fmt.Printf("%v\n", m2) + + if m2.mag() != 10 { + t.Errorf("Mag Error") + } +} + +func TestAdd(t *testing.T) { + p := Point2d{10, 10} + v := Vector2d{3, 4} + m := p.add(v) + + fmt.Printf("%v\n", m) + + if m.X != 13 && m.Y != 14 { + t.Errorf("Add Error") + } +} diff --git a/vectorpoint.go b/vectorpoint.go index aed7143..a4a01dc 100644 --- a/vectorpoint.go +++ b/vectorpoint.go @@ -4,6 +4,13 @@ import ( "math" ) +type Rect2d struct { + X1 float64 `json:"x1"` + Y1 float64 `json:"y1"` + X2 float64 `json:"x2"` + Y2 float64 `json:"y2"` +} + type Vector2d struct { X float64 `json:"x"` Y float64 `json:"y"` @@ -17,10 +24,17 @@ type Point2d struct { const epsilon = 1e-7 func (p1 Point2d) sub(p2 Point2d) Vector2d { - v := Vector2d{p1.X - p2.X, p1.Y - p2.Y} - return v + return Vector2d{p1.X - p2.X, p1.Y - p2.Y} +} + +func (p Point2d) add(v Vector2d) Point2d { + return Point2d{p.X + v.X, p.Y + v.Y} } func (v Vector2d) mag() float64 { return math.Abs(math.Sqrt(v.X*v.X + v.Y*v.Y)) } + +func (v Vector2d) scale(s float64) Vector2d { + return Vector2d{v.X * s, v.Y * s} +}