Description
The LengthSquared
property of the Vector3
struct provides the squared length of the vector. This property is useful for performance optimization when you need to compare vector lengths, as it avoids the computational cost of a square root operation required by the Length
property. The squared length is calculated as x * x + y * y + z * z
.
Usage
Use the LengthSquared
property when you need to compare the lengths of vectors without needing the actual length value. This is particularly useful in scenarios where you are only interested in relative distances, such as determining which of two vectors is longer.
Example
Vector3 vector = new Vector3(3, 4, 5);
float lengthSquared = vector.LengthSquared;
// lengthSquared will be 50, which is 3*3 + 4*4 + 5*5