-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec2f.go
75 lines (53 loc) · 1.21 KB
/
vec2f.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
67
68
69
70
71
72
73
74
75
package kml
import "math"
type Vec2f struct {
X float32
Y float32
}
func (v Vec2f) Equal(v2 Vec2f) bool {
return v.X == v2.X && v.Y == v2.Y
}
func (v *Vec2f) Normalize() float32 {
l := float32(math.Sqrt(float64(v.X*v.X + v.Y*v.Y)))
if l != 0 {
v.X /= l
v.Y /= l
}
return l
}
func (v *Vec2f) Length() float32 {
return float32(math.Sqrt(float64(v.X*v.X + v.Y*v.Y)))
}
func (v1 *Vec2f) Add(v2 Vec2f) Vec2f {
return Vec2f{v1.X + v2.X, v1.Y + v2.Y}
}
func (v1 *Vec2f) Sub(v2 Vec2f) Vec2f {
return Vec2f{v1.X - v2.X, v1.Y - v2.Y}
}
func (v1 *Vec2f) Scale(f float32) Vec2f {
return Vec2f{v1.X * f, v1.Y * f}
}
func (v1 *Vec2f) Mul(v2 Vec2f) Vec2f {
return Vec2f{v1.X * v2.X, v1.Y * v2.Y}
}
func (v1 *Vec2f) Div(f float32) Vec2f {
return Vec2f{v1.X / f, v1.Y / f}
}
func AddVec2f(result, v1, v2 *Vec2f) *Vec2f {
result.X = v1.X + v1.X
result.Y = v1.Y + v1.Y
return result
}
func SubVec2f(result, v1, v2 *Vec2f) *Vec2f {
result.X = v1.X - v1.X
result.Y = v1.Y - v1.Y
return result
}
func DistanceVec2f(v1, v2 *Vec2f) float32 {
lx := v1.X - v2.X
ly := v1.Y - v2.Y
return float32(math.Sqrt(float64(lx*lx + ly*ly)))
}
func DotVec2f(v1 *Vec2f, v2 *Vec2f) float32 {
return ((v1.X * v2.X) + (v1.Y * v2.Y))
}