Description
The DistanceSquared
method calculates the squared distance between two 2D vectors. This method is useful when you need to compare distances without the computational cost of a square root operation, which is required for the actual distance calculation. The squared distance is often used in performance-critical code where the exact distance is not necessary, but a relative comparison is sufficient.
Usage
To use the DistanceSquared
method, pass two Vector2
references as parameters. The method will return a float
representing the squared distance between the two vectors.
Parameters:
a
(ref Vector2
): The first vector.
b
(ref Vector2
): The second vector.
Returns: float
- The squared distance between the two vectors.
Example
Vector2 pointA = new Vector2(1.0f, 2.0f);
Vector2 pointB = new Vector2(4.0f, 6.0f);
float squaredDistance = Vector2.DistanceSquared(ref pointA, ref pointB);
// squaredDistance will be 25.0f