pub struct Vector {
pub x: Fp,
pub y: Fp,
}
Expand description
Represents a vector in a 2D space.
Fields§
§x: Fp
§y: Fp
Implementations§
Source§impl Vector
impl Vector
Sourcepub const fn new(x: Fp, y: Fp) -> Self
pub const fn new(x: Fp, y: Fp) -> Self
Creates a new Vector
with the specified x
and y
components.
§Parameters
x
: The x-coordinate of the vector.y
: The y-coordinate of the vector.
§Returns
A Vector
instance with the given x
and y
components.
§Examples
use fixed32::Fp;
use fixed32_math::Vector;
let v = Vector::new(Fp::from(2), Fp::from(3));
assert_eq!(v.x, Fp::from(2));
assert_eq!(v.y, Fp::from(3));
Sourcepub const fn left() -> Self
pub const fn left() -> Self
Returns a Vector
pointing to the left (negative x-axis direction).
This is a convenience method to create a vector that represents a direction to the left in 2D space.
§Returns
A Vector
instance with x
set to -1
and y
set to 0
.
§Examples
use fixed32::Fp;
use fixed32_math::Vector;
let left = Vector::left();
assert_eq!(left.x, Fp::neg_one());
assert_eq!(left.y, Fp::zero());
Sourcepub const fn right() -> Self
pub const fn right() -> Self
Returns a Vector
pointing to the right (positive x-axis direction).
This is a convenience method to create a vector that represents a direction to the right in 2D space.
§Returns
A Vector
instance with x
set to 1
and y
set to 0
.
§Examples
use fixed32::Fp;
use fixed32_math::Vector;
let right = Vector::right();
assert_eq!(right.x, Fp::one());
assert_eq!(right.y, Fp::zero());
Sourcepub const fn up() -> Self
pub const fn up() -> Self
Returns a Vector
pointing upwards (positive y-axis direction).
This is a convenience method to create a vector that represents a direction upwards in 2D space.
§Returns
A Vector
instance with x
set to 0
and y
set to 1
.
§Examples
use fixed32::Fp;
use fixed32_math::Vector;
let up = Vector::up();
assert_eq!(up.x, Fp::zero());
assert_eq!(up.y, Fp::one());
Sourcepub const fn down() -> Self
pub const fn down() -> Self
Returns a Vector
pointing downwards (negative y-axis direction).
This is a convenience method to create a vector that represents a direction downwards in 2D space.
§Returns
A Vector
instance with x
set to 0
and y
set to -1
.
§Examples
use fixed32::Fp;
use fixed32_math::Vector;
let down = Vector::down();
assert_eq!(down.x, Fp::zero());
assert_eq!(down.y, Fp::neg_one());
Sourcepub fn sqr_len(&self) -> Fp
pub fn sqr_len(&self) -> Fp
Computes the squared length (magnitude) of the vector.
This method calculates the squared length of the vector, which is the sum of the
squares of its x
and y
components. It is often used for performance reasons when
the actual length is not needed, as computing the square root can be costly.
§Returns
The squared length of the vector as an Fp
value.
§Examples
use fixed32::Fp;
use fixed32_math::Vector;
let v = Vector::new(Fp::from(3), Fp::from(4));
let sqr_len = v.sqr_len();
assert_eq!(sqr_len, Fp::from(25));
Sourcepub fn normalize(&self) -> Option<Self>
pub fn normalize(&self) -> Option<Self>
Returns a normalized vector with length 1. Returns None
if the vector is zero-length.
Trait Implementations§
Source§impl AddAssign for Vector
impl AddAssign for Vector
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read more