/* It is the nature of mathematical objects to have many small features * which are not all needed in practice. */ #![allow(dead_code)] use std::ops::{ Add, Sub }; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Alignment { Left, Right } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Side { Top, Bottom, Left, Right } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Corner { TopLeft, TopRight, BottomRight, BottomLeft } impl Side { pub fn opposite(&self) -> Side { match self { Side::Top => Side::Bottom, Side::Right => Side::Left, Side::Bottom => Side::Top, Side::Left => Side::Right, } } pub fn corners_on_side(&self) -> &[Corner] { match self { Side::Top => &[ Corner::TopLeft, Corner::TopRight ], Side::Right => &[ Corner::TopRight, Corner::BottomRight ], Side::Bottom => &[ Corner::BottomRight, Corner::BottomLeft ], Side::Left => &[ Corner::BottomLeft, Corner::TopLeft ], } } } impl Corner { pub fn first_clockwise_side(&self) -> Side { match self { Corner::TopLeft => Side::Left, Corner::TopRight => Side::Top, Corner::BottomRight => Side::Right, Corner::BottomLeft => Side::Bottom, } } } pub trait Coordinate: Copy + From + Ord + Add + Sub { } impl + Ord + Add + Sub> Coordinate for T { } #[derive(Debug)] pub struct Point { pub x: T, pub y: T, } #[derive(Debug)] pub struct Offset { pub x: T, pub y: T, } #[derive(Debug)] pub struct Size { pub width: T, pub height: T, } #[derive(Debug)] pub struct Rectangle { pub left: T, pub top: T, pub width: T, pub height: T, } impl Point { pub fn new(x: T, y: T) -> Point { Point { x, y } } pub fn zero() -> Point { Point { x: 0.into(), y: 0.into(), } } pub fn add_offset>(&self, offset: &Offset) -> Point { Point { x: self.x + offset.x.into(), y: self.y + offset.y.into(), } } pub fn sub_offset>(&self, offset: &Offset) -> Point { Point { x: self.x - offset.x.into(), y: self.y - offset.y.into(), } } pub fn add_size>(&self, size: &Size) -> Point { Point { x: self.x + size.width.into(), y: self.y + size.height.into(), } } pub fn sub_size>(&self, size: &Size) -> Point { Point { x: self.x - size.width.into(), y: self.y - size.height.into(), } } } impl Offset { pub fn new(x: T, y: T) -> Offset { Offset { x, y } } } impl Size { pub fn new(width: T, height: T) -> Size { Size { width, height } } } impl Rectangle { pub fn corner_point(&self, corner: Corner) -> Point { match corner { Corner::TopLeft => Point { x: T::from(0), y: T::from(0) }, Corner::TopRight => Point { x: self.width, y: T::from(0) }, Corner::BottomRight => Point { x: self.width, y: self.height }, Corner::BottomLeft => Point { x: T::from(0), y: self.height }, } } pub fn inset_from_corner_point(&self, corner: Corner, distance: T) -> Point { match corner { Corner::TopLeft => Point { x: distance, y: distance }, Corner::TopRight => Point { x: self.width - distance, y: distance }, Corner::BottomRight => Point { x: self.width - distance, y: self.height - distance }, Corner::BottomLeft => Point { x: distance, y: self.height - distance }, } } pub fn corner_square(&self, corner: Corner, size: T) -> Rectangle { let corner_point = self.corner_point(corner); let inset_point = self.inset_from_corner_point(corner, size); let left = corner_point.x.min(inset_point.x); let top = corner_point.y.min(inset_point.y); let right = corner_point.x.max(inset_point.x); let bottom = corner_point.y.max(inset_point.y); Rectangle { left, top, width: right - left, height: bottom - top, } } }