lengthSq: Squared Vector Length Function
Fast Distance Calculation Without Square Root
The lengthSq function calculates the squared length of a vector using dot product. This avoids the expensive square root operation while preserving distance ordering, making it useful for distance comparisons and performance-critical calculations.
Mathematical Definition: For any vector , the squared length represents energy density:
This operation maintains monotonic ordering properties while eliminating computational complexity, making it ideal for distance comparisons, energy field calculations, and quantum mechanical visualizations.
Distance Field Visualization
This example demonstrates how lengthSq can be used to create distance-based effects from multiple points without the computational cost of square root operations.
ライブエディター
const fragment = () => { const center1 = vec2(0.3, 0.5) const center2 = vec2(-0.2, -0.1) const center3 = vec2(0.1, -0.4) const field1 = lengthSq(uv.sub(center1)).mul(8) const field2 = lengthSq(uv.sub(center2)).mul(12) const field3 = lengthSq(uv.sub(center3)).mul(6) const quantumField = field1.add(field2).add(field3) const energyLevels = quantumField.sin().mul(0.5).add(0.5) const resonance = iTime.mul(0.5).sin().mul(0.2).add(0.8) const finalField = energyLevels.pow(resonance) const redChannel = finalField.mul(2).mod(1.0) const greenChannel = finalField.mul(3).mod(1.0) const blueChannel = finalField.mul(5).mod(1.0) return vec4(redChannel, greenChannel, blueChannel, 1) }
Mathematical Properties and Applications
Property | Mathematical Expression | Computational Advantage |
---|---|---|
Monotonicity | for | Distance comparisons without sqrt |
Scaling Laws | Energy scaling relationships | |
Parallelogram Identity | Physics conservation laws | |
Polarization | Quantum state analysis |