From 848495a631e3724d4b0d3518ebfd3832a35f0705 Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 5 Nov 2017 20:39:30 -0600 Subject: [PATCH] textium: add a Vec2d --- textium/src/geometry.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/textium/src/geometry.rs b/textium/src/geometry.rs index b08584e..0df44c5 100644 --- a/textium/src/geometry.rs +++ b/textium/src/geometry.rs @@ -2,6 +2,7 @@ use std::ops::{Add, Sub, Mul}; +use num_traits::cast::NumCast; /// A rectangle. #[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)] @@ -64,3 +65,33 @@ impl Rect self.bottom() >= other.bottom() } } + +/// A 2D vector +#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)] +pub struct Vec2 { + pub x: T, pub y: T +} + +impl Vec2 { + pub fn new(x: T, y: T) -> Vec2 { + Vec2 { x: x, y: y, } + } +} + +impl From<(T, T)> for Vec2 { + fn from(tuple: (T, T)) -> Self { + Vec2 { + x: tuple.0, + y: tuple.1, + } + } +} + +impl Vec2 where S: NumCast + Copy { + pub fn cast(&self) -> Vec2 where T: NumCast { + Vec2 { + x: NumCast::from(self.x).unwrap(), + y: NumCast::from(self.y).unwrap(), + } + } +}