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§
Sourcefn magnitude(self) -> Self::Scalar
fn magnitude(self) -> Self::Scalar
The euclidean distance between this coordinate and the origin
sqrt(x² + y²)
Sourcefn magnitude_squared(self) -> Self::Scalar
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²
Sourcefn left(self) -> Self
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.
Sourcefn right(self) -> Self
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.
Sourcefn dot_product(self, other: Rhs) -> Self::Scalar
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
Sourcefn wedge_product(self, other: Rhs) -> Self::Scalar
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())
- because it is equivalent to
2D cross product
- because it is equivalent to the signed magnitude of the
conventional 3D cross product assuming
z
ordinates are zero
- because it is equivalent to the signed magnitude of the
conventional 3D cross product assuming
determinant
- because it is equivalent to the
determinant
of the 2x2 matrix formed by the column-vector inputs.
- because it is equivalent to the
§Examples
The following list highlights some examples in geo which might be brought together to use this function:
- geo_types::Point::cross_prod() is already defined on geo_types::Point… but that it seems to be some other operation on 3 points??
- geo_types::Line struct also has a geo_types::Line::determinant()
function which is the same as
line.start.wedge_product(line.end)
- 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”?
- If this is what you are using it for, then please use
crate::algorithm::Kernel::orient2d() instead as this is more
explicit and has a
RobustKernel
option for extra precision.
- If this is what you are using it for, then please use
crate::algorithm::Kernel::orient2d() instead as this is more
explicit and has a
Sourcefn try_normalize(self) -> Option<Self>
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 to0.0
- the vector is really large and the
.magnitude()
has rounded-up or ‘overflowed’ tof64::INFINITY
- Either x or y are
f64::NAN
orf64::INFINITY
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.