Function trySignedAngle

Computes the canonical signed angle from one vector to another.

bool trySignedAngle(T, R)(
  Vector2!T from,
  Vector2!T to,
  out R result
) pure nothrow @nogc @safe
if (isGeoScalar!T && is(R == MetricScalar!T));

Parameters

NameDescription
from Starting direction.
to Target direction.
result Signed angle in radians.

Returns

true when both vectors are finite and exactly non-zero; otherwise false.

On success the result lies in class="pln">LPAREN-pi, pi].

Positive angles correspond to a positive two-dimensional determinant and therefore to counter-clockwise rotation in the conventional Cartesian x-right/y-up orientation.

Both vectors are converted to MetricScalar!T and independently power-of-two scaled before determinant and dot terms are formed. This avoids avoidable product overflow and underflow while preserving the direction represented by each finite vector.

If the computed determinant is exactly zero, the branch cut is canonicalized from the computed dot value:

- dot >= 0 produces positive zero; - dot < 0 produces positive pi.

This is a numerical directional measurement, not a robust orientation or collinearity predicate. No epsilon is used.

On failure, result is zero.

No allocation is performed.

Complexity

O(1) time and O(1) auxiliary space.

Example

Example computing a signed angle through the public package API.

import geo;

auto from = Vector2!int(1, 0);
auto to = Vector2!int(0, 1);

double angle;

assert(
    trySignedAngle(
        from,
        to,
        angle
    )
);

assert(angle > 1.57);
assert(angle < 1.58);