From 3a144c8c1fc83150fc06d792082db5cc4bce3cc5 Mon Sep 17 00:00:00 2001 From: Julian T Date: Tue, 9 Feb 2021 20:00:52 +0100 Subject: Add tiling for rendering --- src/core/vector2.rs | 55 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 17 deletions(-) (limited to 'src/core/vector2.rs') diff --git a/src/core/vector2.rs b/src/core/vector2.rs index 7e40394..0e0165e 100644 --- a/src/core/vector2.rs +++ b/src/core/vector2.rs @@ -2,7 +2,7 @@ //! //! This is implemented generictly with types that fit in the Number trait use crate::{Float, Number}; -use std::ops::{Sub, Add, Mul}; +use std::ops::{Sub, Add, Mul, Div}; use std::fmt; use std::cmp::min; @@ -15,6 +15,7 @@ pub struct Vector2 { pub type Vector2f = Vector2; pub type Vector2i = Vector2; + impl Vector2 { pub fn new(initial: T) -> Vector2 { Vector2 { x: initial, y: initial } @@ -45,6 +46,16 @@ impl Add for Vector2 { } } +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 { @@ -55,25 +66,19 @@ impl Mul for Vector2 { } } -impl fmt::Display for Vector2 { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_fmt(format_args!("[{}, {}]", self.x, self.y)) - } -} - -impl Vector2f { - pub fn ceil(&self) -> Self { +impl Div for Vector2 { + type Output = Self; + fn div(self, op: Self) -> Self::Output { Self::new_xy( - self.x.ceil(), - self.y.ceil() - ) + self.x / op.x, + self.y / op.y, + ) } +} - pub fn floor(&self) -> Self { - Self::new_xy( - self.x.floor(), - self.y.floor() - ) +impl fmt::Display for Vector2 { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_fmt(format_args!("[{}, {}]", self.x, self.y)) } } @@ -96,6 +101,8 @@ impl From for Vector2i { } 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), @@ -108,6 +115,20 @@ impl Vector2f { pub fn len(&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() + ) + } } -- cgit v1.2.3