-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.go
66 lines (52 loc) · 1.22 KB
/
vec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
"math"
)
var UP = Vec2{0, -1}
var DOWN = Vec2{0, 1}
var LEFT = Vec2{-1, 0}
var RIGHT = Vec2{1, 0}
var ZEROZERO = Vec2{0, 0}
var ONEONE = Vec2{1, 1}
type Vec2 struct {
X int `json:"x"`
Y int `json:"y"`
}
func (v Vec2) Add(u Vec2) Vec2 {
return Vec2{X: v.X + u.X, Y: v.Y + u.Y}
}
func (v Vec2) Sub(u Vec2) Vec2 {
return Vec2{X: v.X - u.X, Y: v.Y - u.Y}
}
func (v Vec2) Scale(s int) Vec2 {
return Vec2{X: v.X * s, Y: v.Y * s}
}
func (v Vec2) ManLength() int {
return Absi(v.X) + Absi(v.Y)
}
func (v Vec2) Length() float32 {
return float32(math.Sqrt(float64(v.X*v.X + v.Y*v.Y)))
}
func (v Vec2) Normalized() Vec2 {
var length = v.Length()
return Vec2{
X: int(float32(v.X) / length),
Y: int(float32(v.Y) / length),
}
}
func (v Vec2) CompwiseModulo(d int) Vec2 {
return Vec2{X: v.X % d, Y: v.Y % d}
}
func (v Vec2) CompwiseMul(u Vec2) Vec2 {
return Vec2{X: v.X * u.X, Y: v.Y * u.Y}
}
func (v Vec2) CompwiseMax(u Vec2) Vec2 {
return Vec2{X: max(v.X, u.X), Y: max(v.Y, u.Y)}
}
func (v Vec2) CompwiseMin(u Vec2) Vec2 {
return Vec2{X: min(v.X, u.X), Y: min(v.Y, u.Y)}
}
func (v Vec2) ToRlVec() rl.Vector2 {
return rl.NewVector2(float32(v.X), float32(v.Y))
}