-
Notifications
You must be signed in to change notification settings - Fork 7
/
rotateDeg.go
128 lines (106 loc) · 3.35 KB
/
rotateDeg.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package govec
import "math"
// V2F
// RotateDeg rotates the vector counterclockwise by the specified number of degrees and returns a new vector.
func (v V2F[T]) RotateDeg(degrees float64) V2F[T] {
d := degrees * degToRadFactor
return V2F[T]{
X: T(math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y)),
Y: T(math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Y)),
}
}
// RotateDegInPlace modifies v by rotating the vector counterclockwise by the specified number of degrees.
func (v *V2F[T]) RotateDegInPlace(degrees float64) {
d := degrees * degToRadFactor
x1 := T(math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y))
y1 := T(math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Y))
v.X = x1
v.Y = y1
}
// V2I
// RotateDeg rotates the vector counterclockwise by the specified number of degrees and returns a new V2F vector.
func (v V2I[T]) RotateDeg(degrees float64) V2F[float64] {
d := degrees * degToRadFactor
t := V2F[float64]{
X: math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y),
Y: math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Y),
}
return t
}
// V3F
// RotateDeg rotates the vector counterclockwise by the specified number of degrees, around axis and returns a new vector.
func (v V3F[T]) RotateDeg(degrees float64, axis axis) V3F[T] {
d := degrees * degToRadFactor
switch axis {
case zAxis:
return V3F[T]{
X: T(math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y)),
Y: T(math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Y)),
Z: v.Z,
}
case yAxis:
return V3F[T]{
X: T(math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y)),
Y: v.Y,
Z: T(-math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Z)),
}
default:
return V3F[T]{
X: v.X,
Y: T(math.Cos(d)*float64(v.Y) - math.Sin(d)*float64(v.Z)),
Z: T(-math.Sin(d)*float64(v.Z) + math.Cos(d)*float64(v.Z)),
}
}
}
// RotateDegInPlace modifies v by rotating the vector counterclockwise by the specified number of degrees, around axis.
func (v *V3F[T]) RotateDegInPlace(degrees float64, axis axis) {
d := degrees * degToRadFactor
switch axis {
case zAxis:
x := T(math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y))
y := T(math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Y))
z := T(float64(v.Z))
v.X = x
v.Y = y
v.Z = z
case yAxis:
x := T(math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y))
y := T(float64(v.Y))
z := T(-math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Z))
v.X = x
v.Y = y
v.Z = z
default:
x := T(float64(v.X))
y := T(math.Cos(d)*float64(v.Y) - math.Sin(d)*float64(v.Z))
z := T(-math.Sin(d)*float64(v.Z) + math.Cos(d)*float64(v.Z))
v.X = x
v.Y = y
v.Z = z
}
}
// V3I
// RotateDeg rotates the vector counterclockwise by the specified number of degrees, around axis and returns a new V3F vector.
func (v V3I[T]) RotateDeg(degrees float64, axis axis) V3F[float64] {
d := degrees * degToRadFactor
switch axis {
case zAxis:
return V3F[float64]{
X: math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y),
Y: math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Y),
Z: float64(v.Z),
}
case yAxis:
return V3F[float64]{
X: math.Cos(d)*float64(v.X) - math.Sin(d)*float64(v.Y),
Y: float64(v.Y),
Z: -math.Sin(d)*float64(v.X) + math.Cos(d)*float64(v.Z),
}
default:
return V3F[float64]{
X: float64(v.X),
Y: math.Cos(d)*float64(v.Y) - math.Sin(d)*float64(v.Z),
Z: -math.Sin(d)*float64(v.Z) + math.Cos(d)*float64(v.Z),
}
}
}