Merge unC0Rr's FindPoint() stuff
authorWuzzy <Wuzzy2@mail.ru>
Fri, 02 Nov 2018 17:14:31 +0100
changeset 14107 aa4d6d056e98
parent 14106 6b5e40804ed8 (current diff)
parent 14102 5d42204ac35e (diff)
child 14108 26154fe42974
Merge unC0Rr's FindPoint() stuff
--- a/rust/fpnum/src/lib.rs	Fri Nov 02 17:03:39 2018 +0100
+++ b/rust/fpnum/src/lib.rs	Fri Nov 02 17:14:31 2018 +0100
@@ -55,6 +55,11 @@
     }
 
     #[inline]
+    pub fn abs_round(&self) -> u32 {
+        (self.value >> 32) as u32
+    }
+
+    #[inline]
     pub fn sqr(&self) -> Self {
         Self {
             is_negative: false,
--- a/rust/integral-geometry/Cargo.toml	Fri Nov 02 17:03:39 2018 +0100
+++ b/rust/integral-geometry/Cargo.toml	Fri Nov 02 17:14:31 2018 +0100
@@ -4,3 +4,4 @@
 authors = ["Andrey Korotaev <a.korotaev@hedgewars.org>"]
 
 [dependencies]
+fpnum = { path = "../fpnum" }
--- a/rust/integral-geometry/src/lib.rs	Fri Nov 02 17:03:39 2018 +0100
+++ b/rust/integral-geometry/src/lib.rs	Fri Nov 02 17:14:31 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));
     }
--- a/rust/landgen/src/outline.rs	Fri Nov 02 17:03:39 2018 +0100
+++ b/rust/landgen/src/outline.rs	Fri Nov 02 17:14:31 2018 +0100
@@ -1,4 +1,5 @@
 use itertools::Itertools;
+use std::cmp::min;
 
 use integral_geometry::{Line, Point, Rect, Size};
 use land2d::Land2D;
@@ -57,6 +58,74 @@
         segment: Line,
         random_numbers: &mut I,
     ) -> Option<Point> {
+        let min_distance = 40;
+        // new point should fall inside this box
+        let map_box = self.play_box.with_margin(min_distance);
+
+        let p = Point::new(
+            segment.end.y - segment.start.y,
+            segment.start.x - segment.start.y,
+        );
+        let mid_point = segment.center();
+
+        if (p.integral_norm() < min_distance as u32 * 3) || !map_box.contains_inside(p) {
+            return None;
+        }
+
+        let full_box = Rect::from_size(Point::zero(), self.size).with_margin(min_distance);
+
+        let mut dist_left = (self.size.width + self.size.height) as u32;
+        let mut dist_right = dist_left;
+
+        // find distances to map borders
+        if p.x != 0 {
+            // check against left border
+            let iyl = (map_box.left() - mid_point.x) * p.y / p.x + mid_point.y;
+            let dl = Point::new(mid_point.x - map_box.left(), mid_point.y - iyl).integral_norm();
+            let t = p.x * (mid_point.x - full_box.left()) + p.y * (mid_point.y - iyl);
+
+            if t > 0 {
+                dist_left = dl;
+            } else {
+                dist_right = dl;
+            }
+
+            // right border
+            let iyr = (map_box.right() - mid_point.x) * p.y / p.x + mid_point.y;
+            let dr = Point::new(mid_point.x - full_box.right(), mid_point.y - iyr).integral_norm();
+
+            if t > 0 {
+                dist_right = dr;
+            } else {
+                dist_left = dr;
+            }
+        }
+
+        if p.y != 0 {
+            // top border
+            let ixl = (map_box.top() - mid_point.y) * p.x / p.y + mid_point.x;
+            let dl = Point::new(mid_point.y - map_box.top(), mid_point.x - ixl).integral_norm();
+            let t = p.y * (mid_point.y - full_box.top()) + p.x * (mid_point.x - ixl);
+
+            if t > 0 {
+                dist_left = min(dist_left, dl);
+            } else {
+                dist_right = min(dist_right, dl);
+            }
+
+            // bottom border
+            let ixr = (map_box.bottom() - mid_point.y) * p.x / p.y + mid_point.x;
+            let dr = Point::new(mid_point.y - full_box.bottom(), mid_point.x - ixr).integral_norm();
+
+            if t > 0 {
+                dist_right = min(dist_right, dr);
+            } else {
+                dist_left = min(dist_left, dr);
+            }
+        }
+
+        // now go through all other segments
+
         None
     }