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/bound.rs | 17 +++++++++-------- src/core/vector2.rs | 55 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 25 deletions(-) (limited to 'src/core') diff --git a/src/core/bound.rs b/src/core/bound.rs index a1c1070..404424e 100644 --- a/src/core/bound.rs +++ b/src/core/bound.rs @@ -45,6 +45,15 @@ impl Bound2 { ) } + /// Finds the intersected area between two bounds + pub fn intersect(&self, b: &Bound2) -> Bound2 { + Bound2::new( + &Vector2::new_xy(max(self.min.x, b.min.x), max(self.min.y, b.min.y)), + &Vector2::new_xy(min(self.max.x, b.max.x), min(self.max.y, b.max.y)), + ) + } + + /// Calculates the diagonal vector /// /// Can be used to calculate the size of the bound @@ -96,14 +105,6 @@ impl From<&Bound2f> for Bound2i { } } -/// Finds the intersected area between two bounds -pub fn intersect(a: &Bound2, b: &Bound2) -> Bound2 { - Bound2::new( - &Vector2::new_xy(max(a.min.x, b.min.x), max(a.min.y, b.min.y)), - &Vector2::new_xy(min(a.max.x, b.max.x), min(a.max.y, b.max.y)), - ) -} - #[cfg(test)] mod tests { use super::*; 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