1
0
Fork 0
forked from wry/wry

autocommit 2022-02-04 16:52:11 CET

This commit is contained in:
Julian Orth 2022-02-04 16:52:12 +01:00
parent bb1639a2ae
commit 89bfd2ffcd
21 changed files with 599 additions and 46 deletions

View file

@ -1,5 +1,17 @@
use std::fmt::{Debug, Formatter};
#[derive(Copy, Clone, Eq, PartialEq, Default)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
pub fn translate(&self, x: i32, y: i32) -> (i32, i32) {
(x - self.x, y - self.y)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Default)]
pub struct Rect {
x1: i32,
@ -53,6 +65,13 @@ impl Rect {
}
}
pub fn to_point(&self) -> Point {
Point {
x: self.x1,
y: self.y1,
}
}
pub fn new(x1: i32, y1: i32, x2: i32, y2: i32) -> Option<Self> {
if x2 < x1 || y2 < y1 {
return None;
@ -133,6 +152,19 @@ impl Rect {
}
}
pub fn at_point(&self, x1: i32, y1: i32) -> Self {
Self {
x1,
y1,
x2: x1 + self.x2 - self.x1,
y2: y1 + self.y2 - self.y1,
}
}
pub fn with_size(&self, width: i32, height: i32) -> Option<Self> {
Self::new_sized(self.x1, self.y1, width, height)
}
pub fn translate(&self, x: i32, y: i32) -> (i32, i32) {
(x.wrapping_sub(self.x1), y.wrapping_sub(self.y1))
}