From 3ef8f4d918406eec6bdc29e0ebd883fabfac9b2e Mon Sep 17 00:00:00 2001 From: Julian T Date: Thu, 5 Aug 2021 15:44:40 +0200 Subject: Add picture for c5505ab84820248c6dba35fc06aef9e0ced183de --- src/core/vector2.rs | 167 ---------------------------------------------------- 1 file changed, 167 deletions(-) delete mode 100644 src/core/vector2.rs (limited to 'src/core/vector2.rs') diff --git a/src/core/vector2.rs b/src/core/vector2.rs deleted file mode 100644 index 405b12a..0000000 --- a/src/core/vector2.rs +++ /dev/null @@ -1,167 +0,0 @@ -//! Implements 2d vectors -//! -//! This is implemented generictly with types that fit in the Number trait -use crate::{Float, Number}; -use std::ops::{Sub, Add, Mul, Div}; -use std::fmt; -use std::cmp::min; - -#[derive(Clone, Copy)] -pub struct Vector2 { - pub x: T, - pub y: T, -} - -pub type Vector2f = Vector2; -pub type Vector2i = Vector2; - - -impl Vector2 { - pub fn new(initial: T) -> Vector2 { - Vector2 { x: initial, y: initial } - } - - pub fn new_xy(x: T, y: T) -> Vector2 { - Vector2 { x, y } - } -} - -impl Sub for Vector2 { - type Output = Self; - fn sub(self, op: Self) -> Self::Output { - Self::new_xy( - self.x - op.x, - self.y - op.y, - ) - } -} - -impl Add for Vector2 { - type Output = Self; - fn add(self, op: Self) -> Self::Output { - Self::new_xy( - self.x + op.x, - self.y + op.y, - ) - } -} - -impl Mul for Vector2 { - type Output = Self; - fn mul(self, op: Self) -> Self::Output { - Self::new_xy( - self.x * op.x, - self.y * op.y, - ) - } -} - -impl Mul for Vector2 { - type Output = Self; - fn mul(self, op: T) -> Self::Output { - Self::new_xy( - self.x * op, - self.y * op, - ) - } -} - -impl Div for Vector2 { - type Output = Self; - fn div(self, op: Self) -> Self::Output { - Self::new_xy( - self.x / op.x, - self.y / op.y, - ) - } -} - -impl fmt::Display for Vector2 { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_fmt(format_args!("[{}, {}]", self.x, self.y)) - } -} - -impl From for Vector2f { - fn from(v: Vector2i) -> Self { - Self { - x: v.x as Float, - y: v.y as Float, - } - } -} - -impl From for Vector2i { - fn from(v: Vector2f) -> Self { - Self { - x: v.x as i32, - y: v.y as i32, - } - } -} - -impl Vector2i { - pub const ZERO: Self = Vector2i {x: 0, y: 0}; - - pub fn cap(&self, x: i32, y: i32) -> Self { - Self::new_xy( - min(self.x, x), - min(self.y, y), - ) - } -} - -impl Vector2f { - pub fn length(&self) -> Float { - (self.x*self.x + self.y*self.y).sqrt() - } - - pub fn ceil(&self) -> Self { - Self::new_xy( - self.x.ceil(), - self.y.ceil() - ) - } - - pub fn floor(&self) -> Self { - Self::new_xy( - self.x.floor(), - self.y.floor() - ) - } -} - - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn new_vec2() { - let v = Vector2::new_xy(2.0, 10.0); - - assert!(v.x == 2.0 && v.y == 10.0); - - let v = Vector2::new(3); - - assert!(v.x == 3 && v.y == 3); - } - - #[test] - fn sub_vec2() { - let v1 = Vector2::new_xy(10, 11); - let v2 = Vector2::new_xy(2, 3); - - let v3 = v1-v2; - assert!(v3.x == 8 && v3.y == 8); - } - - #[test] - fn add_vec2() { - let v1 = Vector2::new_xy(10, 11); - let v2 = Vector2::new_xy(2, 3); - - let v3 = v1+v2; - assert!(v3.x == 12 && v3.y == 14); - } -} -- cgit v1.2.3