summaryrefslogtreecommitdiff
path: root/src/dimensions.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2023-12-24 20:03:12 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2023-12-24 21:38:49 +1300
commit8547ad9c2f2867da9b0e655dd4ff0fe78f04ffba (patch)
tree554f4514177cc8c1436ed3ec3ae44f176684f664 /src/dimensions.rs
downloadgeometry-8547ad9c2f2867da9b0e655dd4ff0fe78f04ffba.zip
First commitHEADv1.0.0main
Diffstat (limited to 'src/dimensions.rs')
-rw-r--r--src/dimensions.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/dimensions.rs b/src/dimensions.rs
new file mode 100644
index 0000000..feb5bab
--- /dev/null
+++ b/src/dimensions.rs
@@ -0,0 +1,94 @@
+use crate::*;
+use std::fmt;
+
+#[derive(Copy, Clone, PartialEq)]
+pub struct Dimensions<D: Internal> {
+ pub width: D,
+ pub height: D,
+}
+
+pub trait HasDimensions<D: Internal> {
+ fn dimensions(&self) -> Dimensions<D>;
+
+ fn width(&self) -> D {
+ self.dimensions().width
+ }
+
+ fn height(&self) -> D {
+ self.dimensions().height
+ }
+
+ fn area_usize(&self) -> usize {
+ self.dimensions().width.as_usize() * self.dimensions().height.as_usize()
+ }
+
+ fn area_f64(&self) -> f64 {
+ self.dimensions().width.as_f64() * self.dimensions().height.as_f64()
+ }
+
+ fn contains_point(&self, point: Point<D>) -> bool {
+ point.x < self.width() && point.y < self.height()
+ }
+}
+
+impl<D: Internal> Dimensions<D> {
+ pub const ZERO: Self = Self { width: D::ZERO, height: D::ZERO };
+
+ pub const fn new(width: D, height: D) -> Self {
+ Dimensions { width, height }
+ }
+
+ pub fn is_zero(&self) -> bool {
+ self.width == D::ZERO && self.height == D::ZERO
+ }
+}
+
+impl<D: Internal> HasDimensions<D> for Dimensions<D> {
+ fn dimensions(&self) -> Dimensions<D> {
+ *self
+ }
+}
+
+impl<O: Internal, D: Internal> From<Rect<O, D>> for Dimensions<D> {
+ fn from(rect: Rect<O, D>) -> Self {
+ rect.dimensions
+ }
+}
+
+impl<D: Internal> fmt::Debug for Dimensions<D> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "Dimensions {{ w:{:?} h:{:?} }}", self.width, self.height)
+ }
+}
+
+impl<D: Internal> std::ops::Mul<D> for Dimensions<D> {
+ type Output = Self;
+ fn mul(mut self, scale: D) -> Self {
+ self.width *= scale;
+ self.height *= scale;
+ return self;
+ }
+}
+
+impl<D: Internal> std::ops::MulAssign<D> for Dimensions<D> {
+ fn mul_assign(&mut self, scale: D) {
+ self.width *= scale;
+ self.height *= scale;
+ }
+}
+
+impl<D: Internal> std::ops::Div<D> for Dimensions<D> {
+ type Output = Self;
+ fn div(mut self, scale: D) -> Self {
+ self.width /= scale;
+ self.height /= scale;
+ return self;
+ }
+}
+
+impl<D: Internal> std::ops::DivAssign<D> for Dimensions<D> {
+ fn div_assign(&mut self, scale: D) {
+ self.width /= scale;
+ self.height /= scale;
+ }
+}