You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
594 B
Go
45 lines
594 B
Go
package math
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
type Vec struct {
|
|
X int
|
|
Y int
|
|
}
|
|
|
|
func (v Vec) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal([2]int{v.X, v.Y})
|
|
}
|
|
|
|
func (v *Vec) UnmarshalJSON(b []byte) error {
|
|
var raw [2]int
|
|
if err := json.Unmarshal(b, &raw); err != nil {
|
|
return err
|
|
}
|
|
v.X = raw[0]
|
|
v.Y = raw[1]
|
|
return nil
|
|
}
|
|
|
|
func (v Vec) Add(v2 Vec) Vec { return Vec{v.X + v2.X, v.Y + v2.Y} }
|
|
|
|
func (v Vec) Unit() Vec {
|
|
var out Vec
|
|
switch {
|
|
case v.X < 0:
|
|
out.X = -1
|
|
case v.X > 0:
|
|
out.X = 1
|
|
}
|
|
|
|
switch {
|
|
case v.Y < 0:
|
|
out.Y = -1
|
|
case v.Y > 0:
|
|
out.Y = 1
|
|
}
|
|
return out
|
|
}
|