compute_signed_angle_2d#
- movement.utils.vector.compute_signed_angle_2d(u, v, v_as_left_operand=False)[source]#
Compute the signed angle from the vector
uto the vectorv.The signed angle between
uandvis the rotation that needs to be applied touto have it point in the same direction asv(see Notes). Angles are returned in radians, spanning \((-\pi, \pi]\) according to thearctan2convention.- Parameters:
u (xarray.DataArray) – An array of position vectors containing the
spacedimension with only"x"and"y"coordinates.v (xarray.DataArray | numpy.ndarray) – A 2D vector (or array of 2D vectors) against which to compare
u. May either be an xarray DataArray containing the"space"dimension or a numpy array containing one or more 2D vectors. (See Notes)v_as_left_operand (bool, default False) – If True, the signed angle between
vanduis returned, instead of the signed angle betweenuandv. This is a convenience wrapper for when one of the two vectors to be used does not have time points, and the other does.
- Returns:
An xarray DataArray containing signed angle between
uandvfor every time point. Matches the dimensions ofu, but without thespacedimension.- Return type:
Notes
Given two vectors \(u = (u_x, u_y)\) and \(v = (v_x, v_y)\), the signed angle \(\alpha\) between
uandvis computed as\[\begin{split}\alpha &= \mathrm{arctan2}(u \times v, u\cdot v) \\ &= \mathrm{arctan2}(u_x v_y - u_y v_x, u_x v_x + u_y v_y),\end{split}\]which corresponds to the rotation that needs to be applied to
ufor it to point in the direction ofv.If
vis passed as anxarray.DataArray,vmust have spatial coordinates that match those ofu. Furthermore, any dimensions that are present in bothuandvmust match in length.If passed as a numpy array,
vmust have one of three shapes:(2,): where dimension0contains spatial coordinates (x,y), and no time dimension is specified.(1,2):, where dimension0corresponds to a single time-point and dimension1contains spatial coordinates (x,y).(n,2): where dimension0corresponds to time and dimension1contains spatial coordinates (x,y), and wheren == len(u.time).
Vectors given as
vthat contain more dimensions, or have shapes otherwise different from those defined above are considered invalid.