REPORTER 22.1
Example 2: Magnitude from the Three Vector Components
Example 2: Magnitude from the three vector components
Problem
Given three variables X, Y and Z calculate the vector magnitude and store it in a variable LENGTH.
Solution
JavaScript |
// Get variable values from template |
Discussion
This is done using very similar methods to example 1. The only differences here are using the function Math.sqrt() and we do not use the standard Number.toFixed() function as the length could be smaller than 2 decimal places. Instead we could use Number.toPrecision() or Number.toExponential() if we wanted to format the result instead of leaving it with several decimal places.
The source code for this example is available here .