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§
source§impl<T> Vector2D<T>where
T: FixedWidthSignedInteger,
impl<T> Vector2D<T>where
T: FixedWidthSignedInteger,
source§impl<I, const N: usize> Vector2D<Num<I, N>>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Vector2D<Num<I, N>>where
I: FixedWidthUnsignedInteger,
sourcepub fn trunc(self) -> Vector2D<I>
pub fn trunc(self) -> Vector2D<I>
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);
sourcepub fn floor(self) -> Vector2D<I>
pub fn floor(self) -> Vector2D<I>
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);
Examples found in repository?
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
fn main(mut gba: agb::Gba) -> ! {
let (gfx, mut vram) = gba.display.video.tiled0();
let mut map = gfx.background(
agb::display::Priority::P0,
RegularBackgroundSize::Background32x32,
TileFormat::FourBpp,
);
let mut window = gba.display.window.get();
window
.win_in(WinIn::Win0)
.set_background_enable(map.background(), true)
.set_position(&Rect::new((10, 10).into(), (64, 64).into()))
.enable();
window
.win_out()
.enable()
.set_background_enable(map.background(), true)
.set_blend_enable(true);
example_logo::display_logo(&mut map, &mut vram);
let mut blend = gba.display.blend.get();
blend
.set_background_enable(Layer::Top, map.background(), true)
.set_backdrop_enable(Layer::Bottom, true)
.set_blend_mode(BlendMode::Normal);
let mut pos: Vector2D<FNum> = (10, 10).into();
let mut velocity: Vector2D<FNum> = Vector2D::new(1.into(), 1.into());
let mut blend_amount: Num<i32, 8> = num!(0.5);
let mut blend_velocity: Num<i32, 8> = Num::new(1) / 128;
let vblank = VBlank::get();
loop {
pos += velocity;
if pos.x.floor() > WIDTH - 64 || pos.x.floor() < 0 {
velocity.x *= -1;
}
if pos.y.floor() > HEIGHT - 64 || pos.y.floor() < 0 {
velocity.y *= -1;
}
blend_amount += blend_velocity;
if blend_amount > num!(0.75) || blend_amount < num!(0.25) {
blend_velocity *= -1;
}
blend_amount = blend_amount.clamp(0.into(), 1.into());
blend.set_blend_weight(Layer::Top, blend_amount.try_change_base().unwrap());
window
.win_in(WinIn::Win0)
.set_position(&Rect::new(pos.floor(), (64, 64).into()));
vblank.wait_for_vblank();
window.commit();
blend.commit();
}
}
More examples
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
fn main(mut gba: agb::Gba) -> ! {
let (gfx, mut vram) = gba.display.video.tiled0();
let mut map = gfx.background(
agb::display::Priority::P0,
RegularBackgroundSize::Background32x32,
TileFormat::FourBpp,
);
let mut window = gba.display.window.get();
window
.win_in(WinIn::Win0)
.set_background_enable(map.background(), true)
.set_position(&Rect::new((10, 10).into(), (64, 64).into()))
.enable();
let dmas = gba.dma.dma();
example_logo::display_logo(&mut map, &mut vram);
let mut pos: Vector2D<FNum> = (10, 10).into();
let mut velocity: Vector2D<FNum> = Vector2D::new(1.into(), 1.into());
let vblank = VBlank::get();
let circle: Box<[_]> = (1..64i32)
.map(|i| {
let y = 32 - i;
let x = (32 * 32 - y * y).isqrt();
let x1 = 32 - x;
let x2 = 32 + x;
((x1 as u16) << 8) | (x2 as u16)
})
.collect();
let mut circle_poses = vec![0; 160];
loop {
pos += velocity;
if pos.x.floor() > WIDTH - 64 || pos.x.floor() < 0 {
velocity.x *= -1;
}
if pos.y.floor() > HEIGHT - 64 || pos.y.floor() < 0 {
velocity.y *= -1;
}
let x_pos = pos.x.floor().max(0) as u16;
let y_pos = pos.y.floor().max(0);
let x_adjustment = x_pos << 8 | x_pos;
for (i, value) in circle_poses.iter_mut().enumerate() {
let i = i as i32;
if i <= y_pos || i >= y_pos + 64 {
*value = 0;
continue;
}
*value = circle[(i - y_pos) as usize - 1] + x_adjustment;
}
window
.win_in(WinIn::Win0)
.set_position(&Rect::new(pos.floor(), (64, 65).into()));
window.commit();
let dma_controllable = window.win_in(WinIn::Win0).horizontal_position_dma();
let _transfer = unsafe { dmas.dma0.hblank_transfer(&dma_controllable, &circle_poses) };
vblank.wait_for_vblank();
}
}
sourcepub fn try_change_base<J, const M: usize>(self) -> Option<Vector2D<Num<J, M>>>where
J: FixedWidthUnsignedInteger + TryFrom<I>,
pub fn try_change_base<J, const M: usize>(self) -> Option<Vector2D<Num<J, M>>>where
J: FixedWidthUnsignedInteger + TryFrom<I>,
Attempts to change the base returning None if the numbers cannot be represented
source§impl<const N: usize> Vector2D<Num<i32, N>>
impl<const N: usize> Vector2D<Num<i32, N>>
sourcepub fn magnitude_squared(self) -> Num<i32, N>
pub fn magnitude_squared(self) -> Num<i32, N>
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());
sourcepub fn manhattan_distance(self) -> Num<i32, N>
pub fn manhattan_distance(self) -> Num<i32, N>
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());
sourcepub fn magnitude(self) -> Num<i32, N>
pub fn magnitude(self) -> Num<i32, N>
Calculates the magnitude by square root
let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert_eq!(v1.magnitude(), 5.into());
sourcepub fn fast_magnitude(self) -> Num<i32, N>
pub fn fast_magnitude(self) -> Num<i32, N>
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));
sourcepub fn normalise(self) -> Vector2D<Num<i32, N>>
pub fn normalise(self) -> Vector2D<Num<i32, N>>
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());
sourcepub fn fast_normalise(self) -> Vector2D<Num<i32, N>>
pub fn fast_normalise(self) -> Vector2D<Num<i32, N>>
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());
source§impl<T> Vector2D<T>where
T: Number,
impl<T> Vector2D<T>where
T: Number,
sourcepub fn change_base<U>(self) -> Vector2D<U>where
U: Number + From<T>,
pub fn change_base<U>(self) -> Vector2D<U>where
U: Number + From<T>,
Converts the representation of the vector to another type
let v1: Vector2D<i16> = Vector2D::new(1, 2);
let v2: Vector2D<i32> = v1.change_base();
source§impl<I, const N: usize> Vector2D<Num<I, N>>where
I: FixedWidthSignedInteger,
impl<I, const N: usize> Vector2D<Num<I, N>>where
I: FixedWidthSignedInteger,
source§impl<T> Vector2D<T>where
T: Number,
impl<T> Vector2D<T>where
T: Number,
sourcepub const fn new(x: T, y: T) -> Vector2D<T>
pub const fn new(x: T, y: T) -> Vector2D<T>
Created a vector from the given coordinates
let v = Vector2D::new(1, 2);
assert_eq!(v.x, 1);
assert_eq!(v.y, 2);
Examples found in repository?
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
fn main(mut gba: agb::Gba) -> ! {
let (gfx, mut vram) = gba.display.video.tiled0();
let mut map = gfx.background(
agb::display::Priority::P0,
RegularBackgroundSize::Background32x32,
TileFormat::FourBpp,
);
let mut window = gba.display.window.get();
window
.win_in(WinIn::Win0)
.set_background_enable(map.background(), true)
.set_position(&Rect::new((10, 10).into(), (64, 64).into()))
.enable();
window
.win_out()
.enable()
.set_background_enable(map.background(), true)
.set_blend_enable(true);
example_logo::display_logo(&mut map, &mut vram);
let mut blend = gba.display.blend.get();
blend
.set_background_enable(Layer::Top, map.background(), true)
.set_backdrop_enable(Layer::Bottom, true)
.set_blend_mode(BlendMode::Normal);
let mut pos: Vector2D<FNum> = (10, 10).into();
let mut velocity: Vector2D<FNum> = Vector2D::new(1.into(), 1.into());
let mut blend_amount: Num<i32, 8> = num!(0.5);
let mut blend_velocity: Num<i32, 8> = Num::new(1) / 128;
let vblank = VBlank::get();
loop {
pos += velocity;
if pos.x.floor() > WIDTH - 64 || pos.x.floor() < 0 {
velocity.x *= -1;
}
if pos.y.floor() > HEIGHT - 64 || pos.y.floor() < 0 {
velocity.y *= -1;
}
blend_amount += blend_velocity;
if blend_amount > num!(0.75) || blend_amount < num!(0.25) {
blend_velocity *= -1;
}
blend_amount = blend_amount.clamp(0.into(), 1.into());
blend.set_blend_weight(Layer::Top, blend_amount.try_change_base().unwrap());
window
.win_in(WinIn::Win0)
.set_position(&Rect::new(pos.floor(), (64, 64).into()));
vblank.wait_for_vblank();
window.commit();
blend.commit();
}
}
More examples
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
fn main(mut gba: agb::Gba) -> ! {
let (gfx, mut vram) = gba.display.video.tiled0();
let mut map = gfx.background(
agb::display::Priority::P0,
RegularBackgroundSize::Background32x32,
TileFormat::FourBpp,
);
let mut window = gba.display.window.get();
window
.win_in(WinIn::Win0)
.set_background_enable(map.background(), true)
.set_position(&Rect::new((10, 10).into(), (64, 64).into()))
.enable();
let dmas = gba.dma.dma();
example_logo::display_logo(&mut map, &mut vram);
let mut pos: Vector2D<FNum> = (10, 10).into();
let mut velocity: Vector2D<FNum> = Vector2D::new(1.into(), 1.into());
let vblank = VBlank::get();
let circle: Box<[_]> = (1..64i32)
.map(|i| {
let y = 32 - i;
let x = (32 * 32 - y * y).isqrt();
let x1 = 32 - x;
let x2 = 32 + x;
((x1 as u16) << 8) | (x2 as u16)
})
.collect();
let mut circle_poses = vec![0; 160];
loop {
pos += velocity;
if pos.x.floor() > WIDTH - 64 || pos.x.floor() < 0 {
velocity.x *= -1;
}
if pos.y.floor() > HEIGHT - 64 || pos.y.floor() < 0 {
velocity.y *= -1;
}
let x_pos = pos.x.floor().max(0) as u16;
let y_pos = pos.y.floor().max(0);
let x_adjustment = x_pos << 8 | x_pos;
for (i, value) in circle_poses.iter_mut().enumerate() {
let i = i as i32;
if i <= y_pos || i >= y_pos + 64 {
*value = 0;
continue;
}
*value = circle[(i - y_pos) as usize - 1] + x_adjustment;
}
window
.win_in(WinIn::Win0)
.set_position(&Rect::new(pos.floor(), (64, 65).into()));
window.commit();
let dma_controllable = window.win_in(WinIn::Win0).horizontal_position_dma();
let _transfer = unsafe { dmas.dma0.hblank_transfer(&dma_controllable, &circle_poses) };
vblank.wait_for_vblank();
}
}
sourcepub fn get(self) -> (T, T)
pub fn get(self) -> (T, T)
Returns the tuple of the coordinates
let v = Vector2D::new(1, 2);
assert_eq!(v.get(), (1, 2));
Trait Implementations§
source§impl<T> AddAssign for Vector2D<T>where
T: Number,
impl<T> AddAssign for Vector2D<T>where
T: Number,
source§fn add_assign(&mut self, rhs: Vector2D<T>)
fn add_assign(&mut self, rhs: Vector2D<T>)
+=
operation. Read moresource§impl<T, U> DivAssign<U> for Vector2D<T>where
T: Number<Output = T> + Div<U>,
U: Copy,
impl<T, U> DivAssign<U> for Vector2D<T>where
T: Number<Output = T> + Div<U>,
U: Copy,
source§fn div_assign(&mut self, rhs: U)
fn div_assign(&mut self, rhs: U)
/=
operation. Read moresource§impl<I, const N: usize> From<Vector2D<I>> for Vector2D<Num<I, N>>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> From<Vector2D<I>> for Vector2D<Num<I, N>>where
I: FixedWidthUnsignedInteger,
source§impl<T, U> MulAssign<U> for Vector2D<T>where
T: Number<Output = T> + Mul<U>,
U: Copy,
impl<T, U> MulAssign<U> for Vector2D<T>where
T: Number<Output = T> + Mul<U>,
U: Copy,
source§fn mul_assign(&mut self, rhs: U)
fn mul_assign(&mut self, rhs: U)
*=
operation. Read moresource§impl<T> SubAssign for Vector2D<T>where
T: Number,
impl<T> SubAssign for Vector2D<T>where
T: Number,
source§fn sub_assign(&mut self, rhs: Vector2D<T>)
fn sub_assign(&mut self, rhs: Vector2D<T>)
-=
operation. Read more