vector/vectorpoint.go

50 lines
810 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 21:54:52 -07:00
type Rect2d struct {
X1 float64 `json:"x1"`
Y1 float64 `json:"y1"`
X2 float64 `json:"x2"`
Y2 float64 `json:"y2"`
}
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 22:13:04 -07:00
func (p1 Point2d) Sub(p2 Point2d) Vector2d {
2013-08-08 21:54:52 -07:00
return Vector2d{p1.X - p2.X, p1.Y - p2.Y}
}
2013-08-08 22:13:04 -07:00
func (p Point2d) Add(v Vector2d) Point2d {
2013-08-08 21:54:52 -07:00
return Point2d{p.X + v.X, p.Y + v.Y}
2013-08-08 20:34:27 -07:00
}
2013-08-08 22:13:04 -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))
}
2013-08-08 21:54:52 -07:00
2013-08-08 22:13:04 -07:00
func (v Vector2d) PopPop() float64 {
return v.Mag()
}
func (v Vector2d) Scale(s float64) Vector2d {
2013-08-08 21:54:52 -07:00
return Vector2d{v.X * s, v.Y * s}
}
2013-08-08 23:08:22 -07:00
func (v Vector2d) Normalize() Vector2d {
m := v.Mag()
return Vector2d{v.X / m, v.Y / m}
}