pub struct Vec2 {
pub x: i32,
pub y: i32,
}
Expand description
Represents a two-dimensional spatial vector.
It is generally used as a position vector, representing a point on the
Canvas
. In the terminal, the origin is (by default) set at the
top-left corner with y
increasing downwards, i.e., counting from top to bottom.
At times, it is also used as a size. The differences are shown in the example below.
Example
let mut canvas = Canvas::new(
Vec2::xy(20, 20), // (20, 20) is used as a size here.
&VisualElement::default()
);
let a = Vec2::xy(20, 20); // (20, 20) is used as a position here
let b = Vec2::xy(19, 19); // and (19, 19) as a position here.
assert!(canvas.contains(b)); // b is a valid point on the Canvas.
assert!(!canvas.contains(a)); // The bottom-right corner is actually (19, 19).
Fields§
§x: i32
§y: i32
Implementations§
source§impl Vec2
impl Vec2
sourcepub fn xy<T1: ToPrimitive, T2: ToPrimitive>(x: T1, y: T2) -> Vec2
pub fn xy<T1: ToPrimitive, T2: ToPrimitive>(x: T1, y: T2) -> Vec2
Constructs a Vec2
with the given x
- and y
-coordinates.
sourcepub fn x<T: ToPrimitive>(x: T) -> Vec2
pub fn x<T: ToPrimitive>(x: T) -> Vec2
Constructs a Vec2
with the given x
-coordinate and a y
-coordinate of 0.
sourcepub fn y<T: ToPrimitive>(y: T) -> Vec2
pub fn y<T: ToPrimitive>(y: T) -> Vec2
Constructs a Vec2
with the given y
-coordinate and an x
-coordinate of 0.
Trait Implementations§
source§impl AddAssign<Vec2> for Vec2
impl AddAssign<Vec2> for Vec2
source§fn add_assign(&mut self, other: Vec2)
fn add_assign(&mut self, other: Vec2)
+=
operation. Read moresource§impl<T: ToPrimitive> Div<T> for Vec2
impl<T: ToPrimitive> Div<T> for Vec2
source§impl<T: ToPrimitive> DivAssign<T> for Vec2
impl<T: ToPrimitive> DivAssign<T> for Vec2
source§fn div_assign(&mut self, scalar: T)
fn div_assign(&mut self, scalar: T)
/=
operation. Read moresource§impl DivAssign<Vec2> for Vec2
impl DivAssign<Vec2> for Vec2
source§fn div_assign(&mut self, other: Vec2)
fn div_assign(&mut self, other: Vec2)
/=
operation. Read moresource§impl From<Direction> for Vec2
impl From<Direction> for Vec2
source§fn from(value: Direction) -> Self
fn from(value: Direction) -> Self
Converts a Direction
to a unit-length Vec2
in that direction.
Because terminal coordinates begin at the top line, vertical directions are inverted
compared to what you may expect. More information in the example. For Direction::None
, a
zero Vec2
is returned.
Example
In the terminal, y
increases going downward from the top. Therefore, Direction::Up
is a negative vector and Direction::Down
is a positive vector.
let up = Vec2::from(Direction::Up);
let down = Vec2::from(Direction::Down);
assert_eq!(up, Vec2::y(-1));
assert_eq!(down, Vec2::y(1));
source§impl<T: ToPrimitive> Mul<T> for Vec2
impl<T: ToPrimitive> Mul<T> for Vec2
source§impl<T: ToPrimitive> MulAssign<T> for Vec2
impl<T: ToPrimitive> MulAssign<T> for Vec2
source§fn mul_assign(&mut self, scalar: T)
fn mul_assign(&mut self, scalar: T)
*=
operation. Read moresource§impl MulAssign<Vec2> for Vec2
impl MulAssign<Vec2> for Vec2
source§fn mul_assign(&mut self, other: Vec2)
fn mul_assign(&mut self, other: Vec2)
*=
operation. Read moresource§impl PartialEq<Vec2> for Vec2
impl PartialEq<Vec2> for Vec2
source§impl SubAssign<Vec2> for Vec2
impl SubAssign<Vec2> for Vec2
source§fn sub_assign(&mut self, other: Vec2)
fn sub_assign(&mut self, other: Vec2)
-=
operation. Read moresource§impl TryFrom<Vec2> for Direction
impl TryFrom<Vec2> for Direction
source§fn try_from(value: Vec2) -> Result<Self, Self::Error>
fn try_from(value: Vec2) -> Result<Self, Self::Error>
Converts a Vec2
to a Direction
.
Because terminal coordinates begin at the top line, vertical directions are inverted compared to what you may expect. For more information, see the example.
Errors
If the given value
is not orthogonal, i.e., one of the components is not zero,
TryFromVec2Error
is returned.
Examples
In the terminal, y
increases going downward from the top. Therefore, Direction::Up
is a negative vector and Direction::Down
is a positive vector.
let negative_y = Direction::try_from(Vec2::y(-1)).unwrap();
let positive_y = Direction::try_from(Vec2::y(1)).unwrap();
assert_eq!(negative_y, Direction::Up);
assert_eq!(positive_y, Direction::Down);
Passing non-orthogonal vectors, that is, vectors that are neither parallel to the x
- or
y
-axis will result in an error.
Direction::try_from(Vec2::xy(1, 1)).unwrap(); // panics!