Skip to content

Commit

Permalink
Make Bbox generic
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Mar 10, 2024
1 parent cc077ff commit f4d29b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
47 changes: 28 additions & 19 deletions neothesia-core/src/utils/bbox.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
use std::ops::Add;

use super::{Point, Size};

#[derive(Default, Clone, Copy)]
pub struct Bbox {
pub pos: Point<f32>,
pub size: Size<f32>,
#[derive(Default, Clone, Copy, PartialEq, Eq)]
pub struct Bbox<T = f32> {
pub pos: Point<T>,
pub size: Size<T>,
}

impl Bbox {
pub fn new(pos: Point<f32>, size: Size<f32>) -> Self {
impl<T> Bbox<T> {
pub fn new(pos: Point<T>, size: Size<T>) -> Self {
Self { pos, size }
}
}

pub fn contains(&self, px: f32, py: f32) -> bool {
let Point { x, y } = self.pos;
let Size { w, h } = self.size;

(x..(x + w)).contains(&px) && (y..(y + h)).contains(&py)
impl<T: Clone> Bbox<T> {
pub fn x(&self) -> T {
self.pos.x.clone()
}

pub fn x(&self) -> f32 {
self.pos.x
pub fn y(&self) -> T {
self.pos.y.clone()
}

pub fn y(&self) -> f32 {
self.pos.y
pub fn w(&self) -> T {
self.size.w.clone()
}

pub fn w(&self) -> f32 {
self.size.w
pub fn h(&self) -> T {
self.size.h.clone()
}
}

pub fn h(&self) -> f32 {
self.size.h
impl<T> Bbox<T>
where
T: PartialEq + PartialOrd + Copy + Add<Output = T>,
{
pub fn contains(&self, px: T, py: T) -> bool {
let Point { x, y } = self.pos;
let Size { w, h } = self.size;

(x..(x + w)).contains(&px) && (y..(y + h)).contains(&py)
}
}
4 changes: 2 additions & 2 deletions neothesia-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod resources;

pub use bbox::Bbox;

#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Point<T> {
pub x: T,
pub y: T,
Expand Down Expand Up @@ -57,7 +57,7 @@ where
}
}

#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Size<T> {
pub w: T,
pub h: T,
Expand Down

0 comments on commit f4d29b6

Please sign in to comment.