-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.go
More file actions
72 lines (57 loc) · 1.43 KB
/
vector.go
File metadata and controls
72 lines (57 loc) · 1.43 KB
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
package vector
import "fmt"
// Vector represents a 3D vector
type Vector struct {
i int
j int
k int
}
// New build a valid Vector struct
func New(i int, j int, k int) Vector {
return Vector{i: i, j: j, k: k}
}
// I returns the value of i
func (v Vector) I() int {
return v.i
}
// J returns the value of j
func (v Vector) J() int {
return v.j
}
// K returns the value of k
func (v Vector) K() int {
return v.k
}
// Format return the vector formatted as {i, j, k}
func (v Vector) Format() string {
return fmt.Sprintf("{%d, %d, %d}", v.I(), v.J(), v.K())
}
// Print writes the formatted vector in the output
func (v Vector) Print() {
fmt.Println(v.Format())
}
// DotProduct reproduces the algebraic operation dot product, aka scalar product, resulting in a integer
func (v Vector) DotProduct(v2 Vector) int {
return (v.i * v2.i) + (v.j * v2.j) + (v.k * v2.k)
}
// CrossProduct reproduces the algebraic operation cross product, resulting in an new vector
func (v Vector) CrossProduct(v2 Vector) Vector {
i := (v.j * v2.k) - (v.k * v2.j)
j := (v.k * v2.i) - (v.i * v2.k)
k := (v.i * v2.j) - (v.j * v2.i)
return New(i, j, k)
}
// Add reproduces the addition operation
func (v Vector) Add(v2 Vector) Vector {
i := v.i + v2.i
j := v.j + v2.j
k := v.k + v2.k
return New(i, j, k)
}
// Sub reproduces the subtraction operation
func (v Vector) Sub(v2 Vector) Vector {
i := v.i - v2.i
j := v.j - v2.j
k := v.k - v2.k
return New(i, j, k)
}