roguelike-game/src/rect.rs

45 lines
1018 B
Rust
Raw Normal View History

2022-01-21 15:55:13 -05:00
//! Four points, one plane, 90° angles
2022-01-28 11:48:25 -05:00
use std::collections::HashSet;
2021-12-24 10:38:44 -05:00
use ::serde::{Deserialize, Serialize};
2021-11-08 13:58:40 -05:00
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
2021-10-22 14:50:04 -04:00
pub struct Rect {
pub x1: i32,
pub x2: i32,
pub y1: i32,
pub y2: i32,
}
impl Rect {
pub fn new(x: i32, y: i32, w: i32, h: i32) -> Rect {
2021-10-25 15:26:39 -04:00
Rect {
2021-10-22 14:50:04 -04:00
x1: x,
y1: y,
2021-10-25 15:26:39 -04:00
x2: x + w,
y2: y + h,
2021-10-22 14:50:04 -04:00
}
}
/// Returns true if this overlaps with other
pub fn intersect(&self, other: &Rect) -> bool {
self.x1 <= other.x2 && self.x2 >= other.x1 && self.y1 <= other.y2 && self.y2 >= other.y1
}
pub fn center(&self) -> (i32, i32) {
((self.x1 + self.x2) / 2, (self.y1 + self.y2) / 2)
}
2022-01-28 11:48:25 -05:00
pub fn get_all_tiles(&self) -> HashSet<(i32, i32)> {
let mut result = HashSet::new();
for y in self.y1..self.y2 {
for x in self.x1..self.x2 {
result.insert((x, y));
}
}
result
}
2021-10-25 15:26:39 -04:00
}