View source on GitHub | See Demo | Toggle Types language heron:std:0.1; module heron:geometry.vector:0.1 { // Several algorithms inspired by the following: // https://referencesource.microsoft.com/#System.Numerics/System/Numerics/Vector3 // https://referencesource.microsoft.com/#System.Numerics/System/Numerics/Vector3_Intrinsics.cs // Variables var origin = vector(0, 0, 0); var ones = vector(1, 1, 1); var xaxis = vector(1, 0, 0); var yaxis = vector(0, 1, 0); var zaxis = vector(0, 0, 1); // Functions (Float Float Float -> Float3)function vector(x: Float, y: Float, z: Float) = float3(x, y, z); (Float -> Float3)function vector(x: Float) = vector(x, x, x); /* function vector(xs) = vector(xs[0], xs[1], xs[2]); */ (Float3 -> Array<Float>)function array(v) = [v.x, v.y, v.z]; (Float3 -> Float)function sumComponents(v) = v.x + v.y + v.z; (Float3 Float3 -> Float)function dot(a, b) = sumComponents(a * b); (Float3 -> Float)function length(v) = sqrt(v.length2); (Float3 -> Float)function length2(v) = v.dot(v); (Float3 Float3 -> Float)function distance(a, b) = (a - b).length; (Float3 Float3 -> Float)function distance2(a, b) = (a - b).length2; (Float3 -> Float3)function normal(v) = v / v.length.vector; (Float3 Float3 -> Float3)function cross(a, b) = vector(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); (Float3 Float3 -> Float)function reflect(v, n) = v - (n * dot(v, n) * 2.0); (Float Float Float -> Float)function lerp(a, b, x) = a * (1.0 - x) + b * x; (Float3 -> Float3)function negate(v) = vector(-v.x, -v.y, -v.z); }