2015-05-19 02:41:35 +00:00
|
|
|
//! Points on the 2D character grid.
|
2016-07-13 04:01:11 +00:00
|
|
|
use XY;
|
2016-07-15 03:27:15 +00:00
|
|
|
use direction::Orientation;
|
2015-05-15 19:16:58 +00:00
|
|
|
|
2016-06-28 05:10:59 +00:00
|
|
|
use std::ops::{Add, Div, Mul, Sub};
|
|
|
|
use std::cmp::{Ordering, max, min};
|
2015-05-15 18:58:47 +00:00
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
/// Simple 2D size, in cells.
|
|
|
|
///
|
|
|
|
/// Note: due to a bug in rustdoc ([#32077]), the documentation for `Vec2` is
|
|
|
|
/// currently shown on the [`XY`] page.
|
|
|
|
///
|
|
|
|
/// [#32077]: https://github.com/rust-lang/rust/issues/32077
|
|
|
|
/// [`XY`]: ../struct.XY.html
|
2016-07-13 04:01:11 +00:00
|
|
|
pub type Vec2 = XY<usize>;
|
2015-05-15 18:58:47 +00:00
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
impl PartialOrd for XY<usize> {
|
2016-07-14 05:11:03 +00:00
|
|
|
/// `a < b` <=> `a.x < b.x && a.y < b.y`
|
2015-06-08 03:58:10 +00:00
|
|
|
fn partial_cmp(&self, other: &Vec2) -> Option<Ordering> {
|
2016-03-15 22:37:57 +00:00
|
|
|
if self == other {
|
|
|
|
Some(Ordering::Equal)
|
|
|
|
} else if self.x < other.x && self.y < other.y {
|
|
|
|
Some(Ordering::Less)
|
|
|
|
} else if self.x > other.x && self.y > other.y {
|
|
|
|
Some(Ordering::Greater)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-06-08 03:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
impl XY<usize> {
|
2015-05-19 02:41:35 +00:00
|
|
|
/// Returns a new Vec2 that is a maximum per coordinate.
|
2016-07-13 04:01:11 +00:00
|
|
|
pub fn max<A: Into<Vec2>, B: Into<Vec2>>(a: A, b: B) -> Self {
|
|
|
|
let a = a.into();
|
|
|
|
let b = b.into();
|
2016-07-14 05:11:03 +00:00
|
|
|
a.zip_map(b, max)
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 18:58:47 +00:00
|
|
|
/// Returns a new Vec2 that is no larger than any input in both dimensions.
|
2016-07-13 04:01:11 +00:00
|
|
|
pub fn min<A: Into<Vec2>, B: Into<Vec2>>(a: A, b: B) -> Self {
|
|
|
|
let a = a.into();
|
|
|
|
let b = b.into();
|
2016-07-14 05:11:03 +00:00
|
|
|
a.zip_map(b, min)
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 04:01:11 +00:00
|
|
|
/// Returns the minimum of `self` and `other`.
|
|
|
|
pub fn or_min<T: Into<Vec2>>(self, other: T) -> Self {
|
|
|
|
Vec2::min(self, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the maximum of `self` and `other`.
|
|
|
|
pub fn or_max<T: Into<Vec2>>(self, other: T) -> Self {
|
|
|
|
Vec2::max(self, other)
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:11:55 +00:00
|
|
|
/// Returns a vector with the X component of self, and y=0.
|
2015-05-19 02:41:35 +00:00
|
|
|
pub fn keep_x(&self) -> Self {
|
|
|
|
Vec2::new(self.x, 0)
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:11:55 +00:00
|
|
|
/// Returns a vector with the Y component of self, and x=0.
|
2015-05-19 02:41:35 +00:00
|
|
|
pub fn keep_y(&self) -> Self {
|
|
|
|
Vec2::new(0, self.y)
|
|
|
|
}
|
|
|
|
|
2016-07-15 05:52:37 +00:00
|
|
|
/// Alias for `Vec2::new(0,0)`.
|
2015-05-19 02:41:35 +00:00
|
|
|
pub fn zero() -> Self {
|
2016-03-15 22:37:57 +00:00
|
|
|
Vec2::new(0, 0)
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|
2015-06-08 03:58:10 +00:00
|
|
|
|
|
|
|
/// Returns (max(self.x,other.x), self.y+other.y)
|
|
|
|
pub fn stack_vertical(&self, other: &Vec2) -> Vec2 {
|
|
|
|
Vec2::new(max(self.x, other.x), self.y + other.y)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns (self.x+other.x, max(self.y,other.y))
|
|
|
|
pub fn stack_horizontal(&self, other: &Vec2) -> Vec2 {
|
|
|
|
Vec2::new(self.x + other.x, max(self.y, other.y))
|
|
|
|
}
|
2016-07-13 08:19:05 +00:00
|
|
|
|
|
|
|
/// Returns `true` if `self` could fit inside `other`.
|
|
|
|
///
|
|
|
|
/// Shortcut for `self.x <= other.x && self.y <= other.y`.
|
|
|
|
pub fn fits_in<T: Into<Vec2>>(&self, other: T) -> bool {
|
|
|
|
let other = other.into();
|
|
|
|
self.x <= other.x && self.y <= other.y
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new `Vec2` with the axis `o` set to `value`.
|
2016-07-26 16:55:22 +00:00
|
|
|
pub fn with_axis(&self, o: Orientation, value: usize) -> Self {
|
2016-07-14 03:52:24 +00:00
|
|
|
let mut other = *self;
|
2016-07-13 08:19:05 +00:00
|
|
|
*o.get_ref(&mut other) = value;
|
|
|
|
other
|
|
|
|
}
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
impl From<(i32, i32)> for XY<usize> {
|
2016-07-12 02:24:00 +00:00
|
|
|
fn from((x, y): (i32, i32)) -> Self {
|
|
|
|
(x as usize, y as usize).into()
|
2015-05-20 17:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
impl From<(u32, u32)> for XY<usize> {
|
2016-07-12 02:24:00 +00:00
|
|
|
fn from((x, y): (u32, u32)) -> Self {
|
|
|
|
(x as usize, y as usize).into()
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 22:00:20 +00:00
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
impl<T: Into<Vec2>> Add<T> for XY<usize> {
|
2015-05-15 18:58:47 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
|
2015-05-16 21:02:15 +00:00
|
|
|
fn add(self, other: T) -> Vec2 {
|
2016-07-14 05:11:03 +00:00
|
|
|
self.zip_map(other.into(), Add::add)
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
impl<T: Into<Vec2>> Sub<T> for XY<usize> {
|
2015-05-15 18:58:47 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
|
2015-05-16 21:02:15 +00:00
|
|
|
fn sub(self, other: T) -> Vec2 {
|
2016-07-14 05:11:03 +00:00
|
|
|
self.zip_map(other.into(), Sub::sub)
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-15 23:06:48 +00:00
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
impl Div<usize> for XY<usize> {
|
2015-05-15 23:06:48 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
|
2015-05-25 21:46:29 +00:00
|
|
|
fn div(self, other: usize) -> Vec2 {
|
2016-07-14 05:11:03 +00:00
|
|
|
self.map(|s| s / other)
|
2015-05-15 23:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:18:59 +00:00
|
|
|
impl Mul<usize> for XY<usize> {
|
2015-05-15 23:06:48 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
|
2015-05-25 21:46:29 +00:00
|
|
|
fn mul(self, other: usize) -> Vec2 {
|
2016-07-14 05:11:03 +00:00
|
|
|
self.map(|s| s * other)
|
2015-05-15 23:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-25 21:56:51 +00:00
|
|
|
|
|
|
|
/// Four values representing each direction.
|
2015-06-03 02:36:22 +00:00
|
|
|
#[derive(Clone,Copy)]
|
2015-05-25 21:56:51 +00:00
|
|
|
pub struct Vec4 {
|
|
|
|
/// Left margin
|
|
|
|
pub left: usize,
|
|
|
|
/// Right margin
|
|
|
|
pub right: usize,
|
|
|
|
/// Top margin
|
|
|
|
pub top: usize,
|
|
|
|
/// Bottom margin
|
|
|
|
pub bottom: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Vec4 {
|
|
|
|
/// Creates a new Vec4.
|
|
|
|
pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
|
|
|
|
Vec4 {
|
|
|
|
left: left,
|
|
|
|
right: right,
|
|
|
|
top: top,
|
|
|
|
bottom: bottom,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns left + right.
|
|
|
|
pub fn horizontal(&self) -> usize {
|
|
|
|
self.left + self.right
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns top + bottom.
|
|
|
|
pub fn vertical(&self) -> usize {
|
|
|
|
self.top + self.bottom
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns (left+right, top+bottom).
|
|
|
|
pub fn combined(&self) -> Vec2 {
|
|
|
|
Vec2::new(self.horizontal(), self.vertical())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns (left, top).
|
|
|
|
pub fn top_left(&self) -> Vec2 {
|
|
|
|
Vec2::new(self.left, self.top)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns (right, bottom).
|
|
|
|
pub fn bot_right(&self) -> Vec2 {
|
|
|
|
Vec2::new(self.right, self.bottom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 02:24:00 +00:00
|
|
|
impl From<(usize, usize, usize, usize)> for Vec4 {
|
|
|
|
fn from((left, right, top, bottom): (usize, usize, usize, usize)) -> Vec4 {
|
|
|
|
Vec4::new(left, right, top, bottom)
|
2015-05-25 21:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 02:24:00 +00:00
|
|
|
impl From<(i32, i32, i32, i32)> for Vec4 {
|
|
|
|
fn from((left, right, top, bottom): (i32, i32, i32, i32)) -> Vec4 {
|
|
|
|
(left as usize, right as usize, top as usize, bottom as usize).into()
|
2015-05-25 21:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-03 02:36:22 +00:00
|
|
|
|
2016-07-12 02:24:00 +00:00
|
|
|
impl<T: Into<Vec4>> Add<T> for Vec4 {
|
2015-06-03 02:36:22 +00:00
|
|
|
type Output = Vec4;
|
|
|
|
|
|
|
|
fn add(self, other: T) -> Vec4 {
|
2016-07-12 02:24:00 +00:00
|
|
|
let ov = other.into();
|
2015-06-03 02:36:22 +00:00
|
|
|
|
|
|
|
Vec4 {
|
|
|
|
left: self.left + ov.left,
|
|
|
|
right: self.right + ov.right,
|
|
|
|
top: self.top + ov.top,
|
|
|
|
bottom: self.bottom + ov.bottom,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 02:24:00 +00:00
|
|
|
impl<T: Into<Vec4>> Sub<T> for Vec4 {
|
2015-06-03 02:36:22 +00:00
|
|
|
type Output = Vec4;
|
|
|
|
|
|
|
|
fn sub(self, other: T) -> Vec4 {
|
2016-07-12 02:24:00 +00:00
|
|
|
let ov = other.into();
|
2015-06-03 02:36:22 +00:00
|
|
|
|
|
|
|
Vec4 {
|
|
|
|
left: self.left - ov.left,
|
|
|
|
right: self.right - ov.right,
|
|
|
|
top: self.top - ov.top,
|
|
|
|
bottom: self.bottom - ov.bottom,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Div<usize> for Vec4 {
|
|
|
|
type Output = Vec4;
|
|
|
|
|
|
|
|
fn div(self, other: usize) -> Vec4 {
|
|
|
|
Vec4 {
|
|
|
|
left: self.left / other,
|
|
|
|
right: self.right / other,
|
|
|
|
top: self.top / other,
|
|
|
|
bottom: self.bottom / other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul<usize> for Vec4 {
|
|
|
|
type Output = Vec4;
|
|
|
|
|
|
|
|
fn mul(self, other: usize) -> Vec4 {
|
|
|
|
Vec4 {
|
|
|
|
left: self.left * other,
|
|
|
|
right: self.right * other,
|
|
|
|
top: self.top * other,
|
|
|
|
bottom: self.bottom * other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 04:33:24 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Vec2;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
|
|
|
let vi32 = Vec2::from((4i32, 5i32));
|
|
|
|
let vu32 = Vec2::from((4u32, 5u32));
|
|
|
|
|
|
|
|
let vusize = Vec2::from((4usize, 5usize));
|
|
|
|
let vvec = Vec2::from(Vec2::new(4, 5));
|
|
|
|
|
|
|
|
assert_eq!(vi32 - vu32, vusize - vvec);
|
|
|
|
}
|
|
|
|
}
|