Skip to content

Add ComplexRotation <: Rotation{2} #270

@hyrodium

Description

@hyrodium

We now have two types for 2D rotations:

  • RotMatrix{2} stores four real values as a matrix
  • Angle2d stores one real value as a rotation angle

However, a 2D rotation can be parametrized with two real values $(c,s)$ like

$$ R = \begin{pmatrix}c & -s \\ s & c\end{pmatrix}. $$

This rotation is the same as a rotation by a complex number $c+is$, and can be implemented like this:

struct ComplexRotation{T} <: Rotation{2,T}
    c::Complex{T}
end

This is sometimes useful when we need to generate a rotation from 2D vectors.
See rotation_between for example.

function rotation_between(u::StaticVector{2}, v::StaticVector{2})
c = complex(v[1], v[2]) / complex(u[1], u[2])
iszero(c) && throw(ArgumentError("Input vectors must be nonzero and finite."))
isfinite(c) || throw(ArgumentError("Input vectors must be nonzero and finite."))
theta = Base.angle(c)
return Angle2d(theta)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions