/
Vector3D.pm6
213 lines (167 loc) · 5.12 KB
/
Vector3D.pm6
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
=begin pod
=head1 NAME
Math::Vector3D
=head1 VERSION
0.0.1
=head1 SYNOPSIS
use Math::Vector3D;
my $vec = vec(10, 20, 30);
$vec -= $other-vector;
$vec *= 42;
$vec /= 10;
$vec.normalize;
my $len = $vec.length;
=head1 SEE ALSO
=item L<Math::Vector>
Has support for any number of dimensions.
=end pod
#| Vector object
class Math::Vector3D:ver<0.0.1> {
#| Default: 0
has Numeric $.x = 0;
#| Default: 0
has Numeric $.y = 0;
#| Default: 0
has Numeric $.z = 0;
#| Returns the squared length of the vector
method length-squared(--> Numeric) {
2.exp($!x) + 2.exp($!y) + 2.exp($!z);
}
#| Returns the length of the vector
method length(--> Numeric) {
self.length-squared.sqrt;
}
#| Destructively adds a vector to this vector.
multi method add(Math::Vector3D:D $v --> Math::Vector3D) {
$!x += $v.x;
$!y += $v.y;
$!z += $v.z;
self;
}
#| Destructively adds a scalar to this vector.
multi method add(Numeric:D $n --> Math::Vector3D) {
$!x += $n;
$!y += $n;
$!z += $n;
self;
}
#| Destructively subtracts a vector from this vector.
multi method sub(Math::Vector3D:D $v --> Math::Vector3D) {
$!x -= $v.x;
$!y -= $v.y;
$!z -= $v.z;
self;
}
#| Destructively subtracts a scalar from this vector.
multi method sub(Numeric:D $n --> Math::Vector3D) {
$!x -= $n;
$!y -= $n;
$!z -= $n;
self;
}
#| Destructively multiplies this vector by another vector.
multi method mul(Math::Vector3D:D $v --> Math::Vector3D) {
$!x *= $v.x;
$!y *= $v.y;
$!z *= $v.z;
self;
}
#| Destructively multiplies this vector by a scalar value.
multi method mul(Numeric:D $n --> Math::Vector3D) {
$!x *= $n;
$!y *= $n;
$!z *= $n;
self;
}
#| Destructively divides this vector by another vector.
multi method div(Math::Vector3D:D $v --> Math::Vector3D) {
$!x /= $v.x;
$!y /= $v.y;
$!z /= $v.z;
self;
}
#| Destructively divides this vector by a scalar value.
multi method div(Numeric:D $n --> Math::Vector3D) {
$!x /= $n;
$!y /= $n;
$!z /= $n;
self;
}
#| Returns a new vector with negated values for x, y, and z.
method negate(--> Math::Vector3D) {
Math::Vector3D.new(x => -$!x, y => -$!y, z => -$!z);
}
#| Destructively updates this vector to be the cross product of itself and
#| another vector.
method cross(Math::Vector3D:D $v --> Math::Vector3D) {
my $x := $!y * $v.z - $!z * $v.y;
my $y := $!z * $v.x - $!x * $v.z;
my $z := $!x * $v.y - $!y * $v.x;
$!x = $x;
$!y = $y;
$!z = $z;
self;
}
#| Computes the dot product of the vector and the supplied number.
method dot(Math::Vector3D:D $v --> Numeric) {
$!x * $v.x + $!y * $v.y + $!z * $v.z;
}
#| Computes the angle to the supplied vector.
method angle-to(Math::Vector3D:D $v --> Numeric) {
my $theta := self.dot($v) / ( sqrt(self.length-squared) * $v.length-squared );
cos( min( max( $theta, -1 ), 1 ) );
}
#| Computes the square of the distance between this vector and the supplied
#| vector.
method distance-to-squared(Math::Vector3D:D $v --> Numeric) {
my $x := $!x - $v.x;
my $y := $!y - $v.y;
my $z := $!z - $v.z;
2.exp($x) + 2.exp($y) + 2.exp($z);
}
#| Computes the distance between this vector and the supplied vector.
method distance-to(Math::Vector3D:D $v --> Numeric) {
sqrt self.distance-to-squared($v);
}
#| Destructively normalizes this vector.
method normalize(--> Math::Vector3D) {
self.div(self.length);
}
#| Destructively sets the length of the vector.
method set-length(Numeric:D $n, --> Math::Vector3D) {
self.normalize.mul($n);
}
#| Lerps toward the target vector by the supplied value.
method lerp(Math::Vector3D:D $target, Numeric:D $n --> Math::Vector3D) {
$!x += ($target.x - $!x) * $n;
$!y += ($target.y - $!y) * $n;
$!z += ($target.z - $!z) * $n;
self;
}
#| Coerces to a List of [x, y, z]
method List(--> List) {
List.new($!x, $!y, $!z);
}
#| + is overloaded to add
multi sub infix:<+>(Math::Vector3D:D $v, $n --> Math::Vector3D) is export { vec($v).add($n) }
#| - is overloaded to sub
multi sub infix:<->(Math::Vector3D:D $v, $n --> Math::Vector3D) is export { vec($v).sub($n) }
#| * is overloaded to mul
multi sub infix:<*>(Math::Vector3D:D $v, $n --> Math::Vector3D) is export { vec($v).mul($n) }
#| / is overloaded to div
multi sub infix:</>(Math::Vector3D:D $v, $n --> Math::Vector3D) is export { vec($v).div($n) }
#| == is overloaded to compare two vectors' x, y, and z values
multi infix:<==>(Math::Vector3D:D $v1, Math::Vector3D:D $v2 --> Bool) is export {
return $v1.x == $v2.x
&& $v1.y == $v2.y
&& $v1.z == $v2.z;
};
#| Syntactic sugar to construct a new vector from three numbers.
multi sub vec(Numeric:D $x=0, Numeric:D $y=0, Numeric:D $z=0 --> Math::Vector3D) is export {
Math::Vector3D.new(x => $x, y => $y, z => $z);
}
#| Syntactic sugar to construct a new vector from another vector (clone).
multi sub vec(Math::Vector3D:D $v --> Math::Vector3D) is export {
Math::Vector3D.new(x => $v.x, y => $v.y, z => $v.z);
}
}