Browse Source

textium: add a Vec2d

master
Erin 8 years ago
parent
commit
848495a631
1 changed files with 31 additions and 0 deletions
  1. +31
    -0
      textium/src/geometry.rs

+ 31
- 0
textium/src/geometry.rs View File

@ -2,6 +2,7 @@
use std::ops::{Add, Sub, Mul}; use std::ops::{Add, Sub, Mul};
use num_traits::cast::NumCast;
/// A rectangle. /// A rectangle.
#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)] #[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)]
@ -64,3 +65,33 @@ impl<T> Rect<T>
self.bottom() >= other.bottom() self.bottom() >= other.bottom()
} }
} }
/// A 2D vector
#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)]
pub struct Vec2<T> {
pub x: T, pub y: T
}
impl<T> Vec2<T> {
pub fn new(x: T, y: T) -> Vec2<T> {
Vec2 { x: x, y: y, }
}
}
impl<T> From<(T, T)> for Vec2<T> {
fn from(tuple: (T, T)) -> Self {
Vec2 {
x: tuple.0,
y: tuple.1,
}
}
}
impl<S> Vec2<S> where S: NumCast + Copy {
pub fn cast<T>(&self) -> Vec2<T> where T: NumCast {
Vec2 {
x: NumCast::from(self.x).unwrap(),
y: NumCast::from(self.y).unwrap(),
}
}
}

Loading…
Cancel
Save