pub trait Vector2DOps<Rhs = Self>
where Self: Sized,
{ type Scalar: CoordNum; // Required methods fn magnitude(self) -> Self::Scalar; fn magnitude_squared(self) -> Self::Scalar; fn left(self) -> Self; fn right(self) -> Self; fn dot_product(self, other: Rhs) -> Self::Scalar; fn wedge_product(self, other: Rhs) -> Self::Scalar; fn try_normalize(self) -> Option<Self>; fn is_finite(self) -> bool; }
Expand description

Defines vector operations for 2D coordinate types which implement CoordFloat

This trait is intended for internal use within the geo crate as a way to bring together the various hand-crafted linear algebra operations used throughout other algorithms and attached to various structs.

Required Associated Types§

Required Methods§

source

fn magnitude(self) -> Self::Scalar

The euclidean distance between this coordinate and the origin

sqrt(x² + y²)

source

fn magnitude_squared(self) -> Self::Scalar

The squared distance between this coordinate and the origin. (Avoids the square root calculation when it is not needed)

x² + y²

source

fn left(self) -> Self

Rotate this coordinate around the origin by 90 degrees clockwise.

a.left() => (-a.y, a.x)

Assumes a coordinate system where positive y is up and positive x is to the right. The described rotation direction is consistent with the documentation for crate::algorithm::rotate::Rotate.

source

fn right(self) -> Self

Rotate this coordinate around the origin by 90 degrees anti-clockwise.

a.right() => (a.y, -a.x)

Assumes a coordinate system where positive y is up and positive x is to the right. The described rotation direction is consistent with the documentation for crate::algorithm::rotate::Rotate.

source

fn dot_product(self, other: Rhs) -> Self::Scalar

The inner product of the coordinate components

a · b = a.x * b.x + a.y * b.y

source

fn wedge_product(self, other: Rhs) -> Self::Scalar

The calculates the wedge product between two vectors.

a ∧ b = a.x * b.y - a.y * b.x

Also known as:

  • exterior product
    • because the wedge product comes from ‘Exterior Algebra’
  • perpendicular product
    • because it is equivalent to a.dot(b.right())
  • 2D cross product
    • because it is equivalent to the signed magnitude of the conventional 3D cross product assuming z ordinates are zero
  • determinant
    • because it is equivalent to the determinant of the 2x2 matrix formed by the column-vector inputs.
§Examples

The following list highlights some examples in geo which might be brought together to use this function:

  1. geo_types::Point::cross_prod() is already defined on geo_types::Point… but that it seems to be some other operation on 3 points??
  2. geo_types::Line struct also has a geo_types::Line::determinant() function which is the same as line.start.wedge_product(line.end)
  3. The crate::algorithm::Kernel::orient2d() trait default implementation uses cross product to compute orientation. It returns an enum, not the numeric value which is needed for line segment intersection.
§Properties
  • The absolute value of the cross product is the area of the parallelogram formed by the operands
  • Anti-commutative: The sign of the output is reversed if the operands are reversed
  • If the operands are colinear with the origin, the value is zero
  • The sign can be used to check if the operands are clockwise with respect to the origin, or phrased differently: “is a to the left of the line between the origin and b”?
source

fn try_normalize(self) -> Option<Self>

Try to find a vector of unit length in the same direction as this vector.

Returns None if the result is not finite. This can happen when

  • the vector is really small (or zero length) and the .magnitude() calculation has rounded-down to 0.0
  • the vector is really large and the .magnitude() has rounded-up or ‘overflowed’ to f64::INFINITY
  • Either x or y are f64::NAN or f64::INFINITY
source

fn is_finite(self) -> bool

Returns true if both the x and y components are finite

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T> Vector2DOps for Coord<T>
where T: CoordFloat,

§

type Scalar = T