vector/vectorpoint.go

27 lines
381 B
Go
Raw Normal View History

2013-08-08 20:34:27 -07:00
package govector
import (
2013-08-08 20:36:15 -07:00
"math"
2013-08-08 20:34:27 -07:00
)
2013-08-08 20:36:15 -07:00
type Vector2d struct {
2013-08-08 20:34:27 -07:00
X float64 `json:"x"`
Y float64 `json:"y"`
}
2013-08-08 20:36:15 -07:00
type Point2d struct {
2013-08-08 20:34:27 -07:00
X float64 `json:"x"`
Y float64 `json:"y"`
}
const epsilon = 1e-7
2013-08-08 20:36:15 -07:00
func (p1 Point2d) sub(p2 Point2d) Vector2d {
v := Vector2d{p1.X - p2.X, p1.Y - p2.Y}
2013-08-08 20:34:27 -07:00
return v
}
2013-08-08 20:36:15 -07:00
func (v Vector2d) mag() float64 {
2013-08-08 20:34:27 -07:00
return math.Abs(math.Sqrt(v.X*v.X + v.Y*v.Y))
}