aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-02-09 20:00:52 +0100
committerJulian T <julian@jtle.dk>2021-02-09 20:00:52 +0100
commit3a144c8c1fc83150fc06d792082db5cc4bce3cc5 (patch)
treee408c35a782c8595f2704c4d4dbff3424035ce82 /src/core
parentbb5d37b3685ae8da38865f837cf697dac0055b9d (diff)
Add tiling for rendering
Diffstat (limited to 'src/core')
-rw-r--r--src/core/bound.rs17
-rw-r--r--src/core/vector2.rs55
2 files changed, 47 insertions, 25 deletions
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<T: Number> Bound2<T> {
)
}
+ /// Finds the intersected area between two bounds
+ pub fn intersect(&self, b: &Bound2<T>) -> Bound2<T> {
+ 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<T: Number>(a: &Bound2<T>, b: &Bound2<T>) -> Bound2<T> {
- 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<T: Number> {
pub type Vector2f = Vector2<Float>;
pub type Vector2i = Vector2<i32>;
+
impl<T: Number> Vector2<T> {
pub fn new(initial: T) -> Vector2<T> {
Vector2 { x: initial, y: initial }
@@ -45,6 +46,16 @@ impl<T: Number> Add for Vector2<T> {
}
}
+impl<T: Number> Mul for Vector2<T> {
+ type Output = Self;
+ fn mul(self, op: Self) -> Self::Output {
+ Self::new_xy(
+ self.x * op.x,
+ self.y * op.y,
+ )
+ }
+}
+
impl<T: Number> Mul<T> for Vector2<T> {
type Output = Self;
fn mul(self, op: T) -> Self::Output {
@@ -55,25 +66,19 @@ impl<T: Number> Mul<T> for Vector2<T> {
}
}
-impl<T: Number> fmt::Display for Vector2<T> {
- 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<T: Number> Div for Vector2<T> {
+ 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<T: Number> fmt::Display for Vector2<T> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_fmt(format_args!("[{}, {}]", self.x, self.y))
}
}
@@ -96,6 +101,8 @@ impl From<Vector2f> 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()
+ )
+ }
}