added some stuff

This commit is contained in:
Fraser Graham 2013-08-08 21:54:52 -07:00
parent bf833807f0
commit b5cc7c5aea
2 changed files with 52 additions and 15 deletions

View File

@ -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")
}
}

View File

@ -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}
}