v2 / examples / fireworks / modules / objects / vector.v
28 lines · 22 sloc · 455 bytes · 114a341f5f97855beb888be4b56f43bd9134fcd8
Raw
1module objects
2
3import math
4import rand
5
6pub struct Vector {
7pub mut:
8 x f32
9 y f32
10}
11
12pub fn (a Vector) + (b Vector) Vector {
13 return Vector{a.x + b.x, a.y + b.y}
14}
15
16pub fn (vector Vector) mult(scalar f32) Vector {
17 return Vector{vector.x * scalar, vector.y * scalar}
18}
19
20pub fn random_vector_in_circle() Vector {
21 theta := rand.f32n(2 * math.pi) or { 0 }
22 y := rand.f32()
23
24 return Vector{
25 x: f32(y * math.sin(theta))
26 y: f32(y * math.cos(theta))
27 }
28}
29