-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector2d.go
35 lines (32 loc) · 1.03 KB
/
vector2d.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
package main
import "math"
type Vector2d struct {
x, y float64
}
func (v1 Vector2d) Add(v2 Vector2d) Vector2d {
return Vector2d{v1.x + v2.x, v1.y + v2.y}
}
func (v1 Vector2d) Substract(v2 Vector2d) Vector2d {
return Vector2d{v1.x - v2.x, v1.y - v2.y}
}
func (v1 Vector2d) Multiply(v2 Vector2d) Vector2d {
return Vector2d{v1.x * v2.x, v1.y * v2.y}
}
func (v1 Vector2d) AddValue(value float64) Vector2d {
return Vector2d{v1.x + value, v1.y + value}
}
func (v1 Vector2d) SubstractValue(value float64) Vector2d {
return Vector2d{v1.x - value, v1.y - value}
}
func (v1 Vector2d) MultiplyValue(value float64) Vector2d {
return Vector2d{v1.x * value, v1.y * value}
}
func (v1 Vector2d) DivideValue(value float64) Vector2d {
return Vector2d{v1.x / value, v1.y / value}
}
func (v1 Vector2d) limit(lower, upper float64) Vector2d {
return Vector2d{math.Min(math.Max(v1.x, lower), upper), math.Min(math.Max(v1.y, lower), upper)}
}
func (v1 Vector2d) distance(v2 Vector2d) float64 {
return math.Sqrt(math.Pow(v1.x-v2.x, 2) + math.Pow(v1.y-v2.y, 2))
}