float LengthSquared { get; set; }

book_4_sparkGenerated
code_blocksInput

Description

The LengthSquared property of the Vector2 struct provides the squared length (or magnitude) of the vector. This property is computationally faster than calculating the actual length using the Length property, as it avoids the computational cost of a square root operation. It is particularly useful in scenarios where the exact length is not necessary, such as when comparing distances.

Usage

Use the LengthSquared property when you need to compare the magnitudes of vectors or when the exact length is not required. This can be beneficial in performance-critical code where avoiding the square root calculation can save processing time.

Example

Vector2 vector = new Vector2(3, 4);
float lengthSquared = vector.LengthSquared;
// lengthSquared will be 25, as it is 3*3 + 4*4