--- a/rust/integral-geometry/src/lib.rs Fri Nov 02 15:48:58 2018 +0100
+++ b/rust/integral-geometry/src/lib.rs Fri Nov 02 17:03:58 2018 +0100
@@ -1,3 +1,6 @@
+extern crate fpnum;
+
+use fpnum::distance;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
@@ -38,6 +41,11 @@
}
#[inline]
+ pub fn integral_norm(self) -> u32 {
+ distance(self.x, self.y).abs_round()
+ }
+
+ #[inline]
pub fn transform(self, matrix: &[i32; 4]) -> Self {
Point::new(
matrix[0] * self.x + matrix[1] * self.y,
@@ -210,7 +218,12 @@
}
pub fn from_size(top_left: Point, size: Size) -> Self {
- Rect::new(top_left.x, top_left.y, size.width as u32, size.height as u32)
+ Rect::new(
+ top_left.x,
+ top_left.y,
+ size.width as u32,
+ size.height as u32,
+ )
}
#[inline]
@@ -257,6 +270,14 @@
self.bottom() - margin,
)
}
+
+ #[inline]
+ pub fn contains_inside(&self, point: Point) -> bool {
+ point.x > self.left()
+ && point.x < self.right()
+ && point.y > self.top()
+ && point.y < self.bottom()
+ }
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
@@ -491,6 +512,9 @@
fn rect() {
let r = Rect::from_box(10, 100, 0, 70);
+ assert!(r.contains_inside(Point::new(99, 69)));
+ assert!(!r.contains_inside(Point::new(100, 70)));
+
assert_eq!(r.top_left(), Point::new(10, 0));
assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58));
}