about summary refs log tree commit diff
path: root/src/geometry.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/geometry.rs')
-rw-r--r--src/geometry.rs89
1 files changed, 86 insertions, 3 deletions
diff --git a/src/geometry.rs b/src/geometry.rs
index 3d25b5d..77f7a61 100644
--- a/src/geometry.rs
+++ b/src/geometry.rs
@@ -1,5 +1,12 @@
-use std::ops::Sub;
+/*   It is the nature of mathematical objects to have many small features
+ * which are not all needed in practice.
+ */
+#![allow(dead_code)]
 
+use std::ops::{ Add, Sub };
+
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub enum Alignment { Left, Right }
 
 #[derive(Clone, Copy, PartialEq, Eq)]
 pub enum Side { Top, Bottom, Left, Right }
@@ -39,8 +46,11 @@ impl Corner {
 }
 
 
-pub trait Coordinate: Copy + From<u8> + Ord + Sub<Output=Self> { }
-impl<T: Copy + From<u8> + Ord + Sub<Output=T>> Coordinate for T { }
+pub trait Coordinate: Copy + From<u8> + Ord
+                      + Add<Output=Self> + Sub<Output=Self> { }
+impl<T: Copy + From<u8> + Ord
+        + Add<Output=T> + Sub<Output=T>>
+    Coordinate for T { }
 
 
 #[derive(Debug)]
@@ -50,6 +60,18 @@ pub struct Point<T: Coordinate> {
 }
 
 #[derive(Debug)]
+pub struct Offset<T: Coordinate> {
+  pub x: T,
+  pub y: T,
+}
+
+#[derive(Debug)]
+pub struct Size<T: Coordinate> {
+  pub width: T,
+  pub height: T,
+}
+
+#[derive(Debug)]
 pub struct Rectangle<T: Coordinate> {
   pub left: T,
   pub top: T,
@@ -57,6 +79,67 @@ pub struct Rectangle<T: Coordinate> {
   pub height: T,
 }
 
+impl<T: Coordinate> Point<T> {
+  pub fn new(x: T, y: T) -> Point<T> {
+    Point { x, y }
+  }
+
+  pub fn zero() -> Point<T> {
+    Point {
+      x: 0.into(),
+      y: 0.into(),
+    }
+  }
+
+  pub fn add_offset<U: Coordinate + Into<T>>(&self, offset: &Offset<U>)
+    -> Point<T>
+  {
+    Point {
+      x: self.x + offset.x.into(),
+      y: self.y + offset.y.into(),
+    }
+  }
+
+  pub fn sub_offset<U: Coordinate + Into<T>>(&self, offset: &Offset<U>)
+    -> Point<T>
+  {
+    Point {
+      x: self.x - offset.x.into(),
+      y: self.y - offset.y.into(),
+    }
+  }
+
+  pub fn add_size<U: Coordinate + Into<T>>(&self, size: &Size<U>)
+    -> Point<T>
+  {
+    Point {
+      x: self.x + size.width.into(),
+      y: self.y + size.height.into(),
+    }
+  }
+
+  pub fn sub_size<U: Coordinate + Into<T>>(&self, size: &Size<T>)
+    -> Point<T>
+  {
+    Point {
+      x: self.x - size.width.into(),
+      y: self.y - size.height.into(),
+    }
+  }
+}
+
+impl<T: Coordinate> Offset<T> {
+  pub fn new(x: T, y: T) -> Offset<T> {
+    Offset { x, y }
+  }
+}
+
+impl<T: Coordinate> Size<T> {
+  pub fn new(width: T, height: T) -> Size<T> {
+    Size { width, height }
+  }
+}
+
 impl<T: Coordinate> Rectangle<T> {
   pub fn corner_point(&self, corner: Corner) -> Point<T> {
     match corner {