Struct agb::fixnum::Vector2D

source ·
pub struct Vector2D<T>where
    T: Number,
{ pub x: T, pub y: T, }
Expand description

A vector of two points: (x, y) represented by integers or fixed point numbers

Fields§

§x: T

The x coordinate

§y: T

The y coordinate

Implementations§

Truncates the x and y coordinate, see Num::trunc

let v1: Vector2D<Num<i32, 8>> = (num!(1.56), num!(-2.2)).into();
let v2: Vector2D<i32> = (1, -2).into();
assert_eq!(v1.trunc(), v2);

Floors the x and y coordinate, see Num::floor

let v1: Vector2D<Num<i32, 8>> = Vector2D::new(num!(1.56), num!(-2.2));
let v2: Vector2D<i32> = (1, -3).into();
assert_eq!(v1.floor(), v2);

Attempts to change the base returning None if the numbers cannot be represented

Calculates the magnitude squared, ie (xx + yy)

let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert_eq!(v1.magnitude_squared(), 25.into());

Calculates the manhattan distance, x.abs() + y.abs().

let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert_eq!(v1.manhattan_distance(), 7.into());

Calculates the magnitude by square root

let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert_eq!(v1.magnitude(), 5.into());

Calculates the magnitude of a vector using the alpha max plus beta min algorithm this has a maximum error of less than 4% of the true magnitude, probably depending on the size of your fixed point approximation

let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert!(v1.fast_magnitude() > num!(4.9) && v1.fast_magnitude() < num!(5.1));

Normalises the vector to magnitude of one by performing a square root, due to fixed point imprecision this magnitude may not be exactly one

let v1: Vector2D<Num<i32, 8>> = (num!(4.), num!(4.)).into();
assert_eq!(v1.normalise().magnitude(), 1.into());

Normalises the vector to magnitude of one using Vector2D::fast_magnitude.

let v1: Vector2D<Num<i32, 8>> = (num!(4.), num!(4.)).into();
assert_eq!(v1.fast_normalise().magnitude(), 1.into());

Converts the representation of the vector to another type

let v1: Vector2D<i16> = Vector2D::new(1, 2);
let v2: Vector2D<i32> = v1.change_base();

Creates a unit vector from an angle, noting that the domain of the angle is [0, 1], see Num::cos and Num::sin.

let v: Vector2D<Num<i32, 8>> = Vector2D::new_from_angle(num!(0.0));
assert_eq!(v, (num!(1.0), num!(0.0)).into());

Created a vector from the given coordinates

let v = Vector2D::new(1, 2);
assert_eq!(v.x, 1);
assert_eq!(v.y, 2);

Returns the tuple of the coordinates

let v = Vector2D::new(1, 2);
assert_eq!(v.get(), (1, 2));

Calculates the hadamard product of two vectors

let v1 = Vector2D::new(2, 3);
let v2 = Vector2D::new(4, 5);

let r = v1.hadamard(v2);
assert_eq!(r, Vector2D::new(v1.x * v2.x, v1.y * v2.y));

Swaps the x and y coordinate

let v1 = Vector2D::new(2, 3);
assert_eq!(v1.swap(), Vector2D::new(3, 2));

Trait Implementations§

The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
Performs the /= operation. Read more
Converts to this type from the input type.
Converts to this type from the input type.
The resulting type after applying the * operator.
Performs the * operation. Read more
Performs the *= operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.