1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! Helper `struct`s for abstracting on-screen geometry.


use std::fmt;


/// Represents a single on-screen point/coordinate pair.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Point {
	pub x: i32,
	pub y: i32,
}

impl Point {
	/// Creates a new point on the specified non-negative coordinates
	pub fn new(x: i32, y: i32) -> Point {
		assert!(x >= 0);
		assert!(y >= 0);

		Point{
			x: x,
			y: y
		}
	}
}


/// A 2D size representation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Size {
	pub width: i32,
	pub height: i32,
}

impl Size {
	/// Creates a new non-negative size.
	pub fn new(width: i32, height: i32) -> Size {
		assert!(width >= 0);
		assert!(height >= 0);

		Size{
			width: width,
			height: height,
		}
	}
}

impl fmt::Display for Size {
	fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
		write!(formatter, "{}x{}", self.width, self.height)
	}
}


/// A rectangle, described by its four corners and a size.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Rect {
	/// The top-left corner.
	pub top_left: Point,
	/// The top-right corner.
	pub top_right: Point,
	/// The bottom-right corner.
	pub bottom_right: Point,
	/// The bottom-left corner.
	pub bottom_left: Point,
	/// The `Rect`angle's size.
	pub size: Size,
}

impl Rect {
	/// Construct a `Rect` from its top-left corner and its size.
	///
	/// # Examples
	///
	/// ```
	/// # use bear_lib_terminal::geometry::{Rect, Point, Size};
	/// let rect = Rect::from_size(Point::new(10, 20), Size::new(30, 40));
	/// assert_eq!(rect.top_left, Point::new(10, 20));
	/// assert_eq!(rect.top_right, Point::new(40, 20));
	/// assert_eq!(rect.bottom_left, Point::new(10, 60));
	/// assert_eq!(rect.bottom_right, Point::new(40, 60));
	/// assert_eq!(rect.size, Size::new(30, 40));
	/// ```
	pub fn from_size(origin: Point, size: Size) -> Rect {
		let top_right    = Point::new(origin.x + size.width, origin.y);
		let bottom_left  = Point::new(origin.x, origin.y + size.height);
		let bottom_right = Point::new(top_right.x, bottom_left.y);

		Rect{
			top_left: origin,
			top_right: top_right,
			bottom_left: bottom_left,
			bottom_right: bottom_right,
			size: size
		}
	}

	/// Construct a `Rect` from its top-left and bottom-right corners.
	///
	/// # Examples
	///
	/// ```
	/// # use bear_lib_terminal::geometry::{Rect, Point, Size};
	/// let rect = Rect::from_points(Point::new(10, 20), Point::new(30, 40));
	/// assert_eq!(rect.top_left, Point::new(10, 20));
	/// assert_eq!(rect.top_right, Point::new(30, 20));
	/// assert_eq!(rect.bottom_left, Point::new(10, 40));
	/// assert_eq!(rect.bottom_right, Point::new(30, 40));
	/// assert_eq!(rect.size, Size::new(20, 20));
	/// ```
	pub fn from_points(top_left: Point, bottom_right: Point) -> Rect {
		assert!(bottom_right.x >= top_left.x);
		assert!(bottom_right.y >= top_left.y);

		let size = Size::new(bottom_right.x - top_left.x, bottom_right.y - top_left.y);
		Rect::from_size(top_left, size)
	}

	/// Construct a `Rect` from its top-left corner and its size, values unwrapped.
	///
	/// # Examples
	///
	/// ```
	/// # use bear_lib_terminal::geometry::{Rect, Point, Size};
	/// assert_eq!(Rect::from_values(10, 20, 30, 40), Rect::from_size(Point::new(10, 20), Size::new(30, 40)));
	/// ```
	pub fn from_values(x: i32, y: i32, width: i32, height: i32) -> Rect {
		let origin = Point::new(x, y);
		let size = Size::new(width, height);
		Rect::from_size(origin, size)
	}


	/// Construct a `Rect` from its top-left and bottom-right corners, values unwrapped.
	///
	/// # Examples
	///
	/// ```
	/// # use bear_lib_terminal::geometry::{Rect, Point, Size};
	/// assert_eq!(Rect::from_point_values(10, 20, 30, 40), Rect::from_points(Point::new(10, 20), Point::new(30, 40)));
	/// ```
	pub fn from_point_values(top_left_x: i32, top_left_y: i32, bottom_right_x: i32, bottom_right_y: i32) -> Rect {
		let top_left = Point::new(top_left_x, top_left_y);
		let bottom_right = Point::new(bottom_right_x, bottom_right_y);
		Rect::from_points(top_left, bottom_right)
	}
}