mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
examples: improve the pendulum simulation, with several modes and diagrams (#13446)
This commit is contained in:
parent
a74d28ae5f
commit
4391ae563d
26 changed files with 1501 additions and 366 deletions
50
examples/pendulum-simulation/modules/sim/vec.v
Normal file
50
examples/pendulum-simulation/modules/sim/vec.v
Normal file
|
@ -0,0 +1,50 @@
|
|||
module sim
|
||||
|
||||
import math
|
||||
|
||||
// Vector3D is a 3D vector
|
||||
pub struct Vector3D {
|
||||
x f64
|
||||
y f64
|
||||
z f64
|
||||
}
|
||||
|
||||
// vector creates a Vector3D passing x,y,z as parameteres
|
||||
pub fn vector(data Vector3D) Vector3D {
|
||||
return Vector3D{
|
||||
...data
|
||||
}
|
||||
}
|
||||
|
||||
// addition
|
||||
pub fn (v Vector3D) + (v2 Vector3D) Vector3D {
|
||||
return Vector3D{
|
||||
x: v.x + v2.x
|
||||
y: v.y + v2.y
|
||||
z: v.z + v2.z
|
||||
}
|
||||
}
|
||||
|
||||
// dot product
|
||||
pub fn (v Vector3D) * (v2 Vector3D) f64 {
|
||||
return (v.x * v2.x) + (v.y * v2.y) + (v.z * v2.z)
|
||||
}
|
||||
|
||||
// scale gets a scaled vector
|
||||
pub fn (v Vector3D) scale(scalar f64) Vector3D {
|
||||
return Vector3D{
|
||||
x: v.x * scalar
|
||||
y: v.y * scalar
|
||||
z: v.z * scalar
|
||||
}
|
||||
}
|
||||
|
||||
// norm_squared returns the square of the norm of the vector
|
||||
pub fn (v Vector3D) norm_squared() f64 {
|
||||
return v * v
|
||||
}
|
||||
|
||||
// norm returns the norm of the vector
|
||||
pub fn (v Vector3D) norm() f64 {
|
||||
return math.sqrt(v.norm_squared())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue