-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvector.go
executable file
·59 lines (47 loc) · 1.25 KB
/
vector.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
//This file contains some useful vector operators
package vinamax
import "math"
type vector [3]float64
//Dot product between two vectors
func (x vector) dot(y vector) float64 {
return (x[0]*y[0] + x[1]*y[1] + x[2]*y[2])
}
//cross product between two vectors
func (x *vector) cross(y vector) vector {
return vector{x[1]*y[2] - x[2]*y[1], y[0]*x[2] - y[2]*x[0], x[0]*y[1] - x[1]*y[0]}
}
//Set norm of a vector to one
// uses the taylor expansion of sqrt because it's close to 1 anyway, and adds lots of speed
func norm(x vector) vector {
magnitude := math.Sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2])
// magnitude := ((x[0]*x[0]+x[1]*x[1]+x[2]*x[2])-1)/2. + 1
return x.times(1. / magnitude)
}
//returns the norm of a vector
func size(x vector) float64 {
return math.Sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2])
}
//multiply each component of a vector by a float
func (x vector) times(i float64) vector {
return vector{x[0] * i, x[1] * i, x[2] * i}
}
//Add two vectors
func (x vector) add(i vector) vector {
x[0] += i[0]
x[1] += i[1]
x[2] += i[2]
return x
}
func (x *vector) directadd(i vector) {
x[0] += i[0]
x[1] += i[1]
x[2] += i[2]
}
//cubes a number
func cube(x float64) float64 {
return x * x * x
}
//squares a number
func sqr(x float64) float64 {
return x * x
}