Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
97
crates/domain/src/value_objects.rs
Normal file
97
crates/domain/src/value_objects.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
||||
pub struct Point2D {
|
||||
x: f32,
|
||||
y: f32,
|
||||
}
|
||||
|
||||
impl Point2D {
|
||||
pub const fn new(x: f32, y: f32) -> Option<Self> {
|
||||
if x.is_finite() && y.is_finite() {
|
||||
Some(Self { x, y })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn x(&self) -> f32 {
|
||||
self.x
|
||||
}
|
||||
|
||||
pub const fn y(&self) -> f32 {
|
||||
self.y
|
||||
}
|
||||
|
||||
pub(crate) fn translated(self, by: Vector2D) -> Option<Self> {
|
||||
Self::new(self.x + by.dx, self.y + by.dy)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Vector2D {
|
||||
dx: f32,
|
||||
dy: f32,
|
||||
}
|
||||
|
||||
impl Vector2D {
|
||||
pub const fn new(dx: f32, dy: f32) -> Option<Self> {
|
||||
if dx.is_finite() && dy.is_finite() {
|
||||
Some(Self { dx, dy })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn dx(&self) -> f32 {
|
||||
self.dx
|
||||
}
|
||||
|
||||
pub const fn dy(&self) -> f32 {
|
||||
self.dy
|
||||
}
|
||||
|
||||
pub(crate) const fn reflected_x(self) -> Self {
|
||||
Self {
|
||||
dx: -self.dx,
|
||||
dy: self.dy,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const fn reflected_y(self) -> Self {
|
||||
Self {
|
||||
dx: self.dx,
|
||||
dy: -self.dy,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Dimensions {
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
impl Dimensions {
|
||||
pub const fn new(width: u32, height: u32) -> Option<Self> {
|
||||
if width == 0 || height == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(Self { width, height })
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn width(&self) -> u32 {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub const fn height(&self) -> u32 {
|
||||
self.height
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Color {
|
||||
pub r: u8,
|
||||
pub g: u8,
|
||||
pub b: u8,
|
||||
pub a: u8,
|
||||
}
|
||||
Reference in New Issue
Block a user