Description
The LengthSquared
property of the Vector2Int
struct provides the squared length of the integer vector. This property is useful for performance optimization as it avoids the computational cost of a square root operation, which is required when calculating the actual length. It is particularly beneficial when comparing distances, as long as the comparison is done using squared values.
Usage
Use the LengthSquared
property when you need to compare the magnitude of vectors without needing the exact length. This is especially useful in scenarios where performance is critical, such as in game development or real-time simulations.
Example
Vector2Int vector = new Vector2Int(3, 4);
int squaredLength = vector.LengthSquared;
// squaredLength will be 25, as 3*3 + 4*4 = 9 + 16 = 25
// Use LengthSquared for comparison
Vector2Int anotherVector = new Vector2Int(5, 0);
if (vector.LengthSquared > anotherVector.LengthSquared)
{
// vector is longer than anotherVector in terms of squared length
}