pub struct Vector2<T> {
pub x_: T,
pub y_: T,
}
Expand description
The Vector2
struct represents a 2-dimensional vector with elements of type T
.
Properties:
x_
: Thex_
property represents the first element of theVector2
object. It is of typeT
, which means it can be any type that is specified when creating an instance ofVector2
.y_
: They_
property is the second element of theVector2
object. It represents the y-coordinate of a 2D vector.
§Examples:
use ginger::vector2::Vector2;
assert_eq!(Vector2::new(3, 4), Vector2 { x_: 3, y_: 4});
Fields§
§x_: T
The first element of the vector2 object
y_: T
The second element of the vector2 object
Implementations§
Source§impl<T> Vector2<T>
impl<T> Vector2<T>
Sourcepub const fn new(x_: T, y_: T) -> Self
pub const fn new(x_: T, y_: T) -> Self
Creates a new Vector2<T>
.
The new
function creates a new Vector2
instance with the given x
and y
values.
Arguments:
x_
: The parameterx_
represents the x-coordinate of the vector. It is of typeT
, which means it can be any type that implements the necessary operations for vector calculations (e.g., addition, subtraction, multiplication).y_
: They_
parameter represents the y-coordinate of the vector.
Returns:
The new
function returns a new instance of the Vector2<T>
struct.
§Examples
use ginger::vector2::Vector2;
assert_eq!(Vector2::new(3, 4), Vector2 { x_: 3, y_: 4});
Source§impl<T: Clone + Num> Vector2<T>
impl<T: Clone + Num> Vector2<T>
Sourcepub fn dot(&self, other: &Self) -> T
pub fn dot(&self, other: &Self) -> T
The dot
function calculates the dot product of two vectors.
Arguments:
other
: Theother
parameter is a reference to anotherVector2
object that we want to calculate the dot product with.
Returns:
The dot product of two vectors is being returned.
§Examples
use ginger::vector2::Vector2;
let vector2 = &Vector2::new(3, 4);
let other = &Vector2::new(5, 6);
assert_eq!(vector2.dot(other), 15 + 24);
assert_eq!(vector2.dot(&vector2), 9 + 16);
Sourcepub fn cross(&self, other: &Self) -> T
pub fn cross(&self, other: &Self) -> T
The cross
function calculates the cross product of two vectors.
Arguments:
other
: Theother
parameter is a reference to anotherVector2
object that we want to calculate the cross product with.
Returns:
The cross product of two vectors is being returned.
§Examples
use ginger::vector2::Vector2;
let vector2 = &Vector2::new(3, 4);
let other = &Vector2::new(5, 6);
assert_eq!(vector2.cross(other), 18 - 20);
assert_eq!(vector2.cross(&vector2), 0);
Sourcepub fn norm_sqr(&self) -> T
pub fn norm_sqr(&self) -> T
Returns the norm sqr of this Vector2<T>
.
The norm_sqr
function calculates the squared norm of a Vector2
object.
Returns:
The norm_sqr
function returns the squared norm of the Vector2<T>
.
§Examples
use ginger::vector2::Vector2;
let vector2 = &Vector2::new(3, 4);
assert_eq!(vector2.norm_sqr(), 9 + 16);
Sourcepub fn scale(&self, alpha: T) -> Self
pub fn scale(&self, alpha: T) -> Self
The scale
function multiplies the x and y components of a Vector2
object by a given scalar
value.
Arguments:
alpha
: The parameteralpha
represents the scaling factor that will be applied to the vector.
Returns:
The scale
method returns a new Vector2
object.
§Examples
use ginger::vector2::Vector2;
let vector2 = &Vector2::new(3.0, 4.0);
assert_eq!(vector2.scale(10.0), Vector2::new(30.0, 40.0));
assert_eq!(vector2.scale(0.5), Vector2::new(1.5, 2.0));
Sourcepub fn unscale(&self, alpha: T) -> Self
pub fn unscale(&self, alpha: T) -> Self
The unscale
function divides the x and y components of a Vector2
by a given value.
Arguments:
alpha
: Thealpha
parameter is a value of typeT
that is used to divide thex_
andy_
values of theVector2
object.
Returns:
The unscale
method returns a new Vector2
object.
§Examples
use ginger::vector2::Vector2;
let vector2 = &Vector2::new(30, 40);
assert_eq!(vector2.unscale(10), Vector2::new(3, 4));
Source§impl<T: Clone + Signed> Vector2<T>
impl<T: Clone + Signed> Vector2<T>
Sourcepub fn l1_norm(&self) -> T
pub fn l1_norm(&self) -> T
The l1_norm
function calculates the Manhattan distance from the origin for a 2D vector.
Returns:
The function l1_norm
returns the L1 norm of a Vector2
object, which is the sum of the absolute
values of its x_
and y_
components.
§Examples
use ginger::vector2::Vector2;
let vector2 = &Vector2::new(3, -4);
assert_eq!(vector2.l1_norm(), 7);
Source§impl<T: Clone + PartialOrd> Vector2<T>
impl<T: Clone + PartialOrd> Vector2<T>
Sourcepub fn norm_inf(&self) -> T
pub fn norm_inf(&self) -> T
The norm_inf
function returns the maximum absolute value of the two elements in a Vector2
object.
Returns:
The norm_inf
function returns the maximum value between self.x_
and self.y_
.
§Examples
use ginger::vector2::Vector2;
let vector2 = &Vector2::new(3, -4);
assert_eq!(vector2.norm_inf(), 3);
Trait Implementations§
Source§impl<'a, T: Clone + NumAssign> AddAssign<&'a Vector2<T>> for Vector2<T>
impl<'a, T: Clone + NumAssign> AddAssign<&'a Vector2<T>> for Vector2<T>
Source§fn add_assign(&mut self, other: &Self)
fn add_assign(&mut self, other: &Self)
+=
operation. Read moreSource§impl<T: Clone + NumAssign> AddAssign for Vector2<T>
impl<T: Clone + NumAssign> AddAssign for Vector2<T>
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+=
operation. Read moreSource§impl<'a, T: Clone + NumAssign> DivAssign<&'a T> for Vector2<T>
impl<'a, T: Clone + NumAssign> DivAssign<&'a T> for Vector2<T>
Source§fn div_assign(&mut self, other: &T)
fn div_assign(&mut self, other: &T)
/=
operation. Read moreSource§impl<T: Clone + NumAssign> DivAssign<T> for Vector2<T>
impl<T: Clone + NumAssign> DivAssign<T> for Vector2<T>
Source§fn div_assign(&mut self, other: T)
fn div_assign(&mut self, other: T)
/=
operation. Read moreSource§impl<'a, T: Clone + NumAssign> MulAssign<&'a T> for Vector2<T>
impl<'a, T: Clone + NumAssign> MulAssign<&'a T> for Vector2<T>
Source§fn mul_assign(&mut self, other: &T)
fn mul_assign(&mut self, other: &T)
*=
operation. Read moreSource§impl<T: Clone + NumAssign> MulAssign<T> for Vector2<T>
impl<T: Clone + NumAssign> MulAssign<T> for Vector2<T>
Source§fn mul_assign(&mut self, other: T)
fn mul_assign(&mut self, other: T)
*=
operation. Read moreSource§impl<'a, T: Clone + NumAssign> SubAssign<&'a Vector2<T>> for Vector2<T>
impl<'a, T: Clone + NumAssign> SubAssign<&'a Vector2<T>> for Vector2<T>
Source§fn sub_assign(&mut self, other: &Self)
fn sub_assign(&mut self, other: &Self)
-=
operation. Read moreSource§impl<T: Clone + NumAssign> SubAssign for Vector2<T>
impl<T: Clone + NumAssign> SubAssign for Vector2<T>
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
-=
operation. Read more