Function tryNormalize
Constructs the unit direction of a finite non-zero vector.
Parameters
| Name | Description |
|---|---|
| vector | Source vector. |
| result | Unit vector in MetricScalar!T. |
Returns
true when a finite unit direction is produced; otherwise false.
Returns false when either component is non-finite or when the vector is
exactly zero. No epsilon is used.
On failure, result is Vector2!R.
Components are converted to the metric computation type before absolute
magnitude is formed. The converted vector is then scaled by its largest
absolute component before hypot is evaluated. This avoids signed
integral T negation and avoids unnecessary overflow or underflow
for extreme finite vectors.
No allocation is performed.
Complexity
O(1) time and O(1) auxiliary space.
Example
Example normalizing a vector through the public package API.
import geo;
auto vector = Vector2!int(3, 4);
Vector2!double unit;
assert(
tryNormalize(
vector,
unit
)
);
assert(
unit ==
Vector2!double(
0.6,
0.8
)
);