chiark / gitweb /
Provide distinction betwene Region and Area
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Mar 2021 01:43:59 +0000 (01:43 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Mar 2021 11:02:58 +0000 (11:02 +0000)
The latter will always be rectangle.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
base/geometry.rs
src/hand.rs
src/hidden.rs
src/prelude.rs
src/shapelib.rs

index 2e1708ce642d8d4a60795afc06db565f92f9d291..164b0fe7ae4e8fd5e134844907caa23d3e7c58b9 100644 (file)
@@ -251,4 +251,16 @@ impl<T> Region<T> {
       Rectangle(a) => a.contains(pos),
     }
   }
+
+  pub fn overlaps(&self, other: &Region<T>) -> bool where T: PartialOrd {
+    use Region::*;
+    match (self, other) {
+      (Rectangle(a), Rectangle(b)) => a.overlaps(b)
+    }
+  }
+
+  pub fn empty() -> Self where T: Copy + num_traits::Zero + num_traits::One {
+    Region::Rectangle(AreaC::empty())
+  }
+
 }
index 5db54ad38fa22cfede72a971d512dab8737147eb..a6c2b7a663ef7651d9f6a4a687227840a91e4bbe 100644 (file)
@@ -187,8 +187,9 @@ impl PieceTrait for Hand {
         });
         let (region, views) = (||{
           dbgc!("claiming region");
+          let area   = self.shape.outline.area  (gpc.pos)?;
           let region = self.shape.outline.region(gpc.pos)?;
-          let displace = OccDisplacement::Rect { area: region };
+          let displace = OccDisplacement::Rect { area };
           let views = OwnerOccultationView {
             owner: player,
             owner_view: OccK::Visible,
index 289e3c009b15ee409f95c201aec3b3937ac0701b..e66643a9b6bb6fcec64f221b9ab53e567dd85881 100644 (file)
@@ -43,7 +43,7 @@ struct Passive {
 
 #[derive(Clone,Debug,Serialize,Deserialize)]
 pub struct Occultation {
-  region: Area, // automatically affect pieces here
+  region: Region<Coord>, // automatically affect pieces here
   occulter: PieceId, // kept in synch with PieceOccult::active
   notches: Notches, // kept in synch with PieceOccult::passive
   ppiece_use_size: Pos, // taken from first piece
@@ -812,7 +812,7 @@ pub fn create_occultation(
   ipieces: &IPieces,
   ioccults: &IOccults,
   to_recalculate: &mut ToRecalculate,
-  region: Area,
+  region: Region<Coord>,
   occulter: PieceId,
   views: OccultationViews,
 ) -> Vec<(PieceId, PieceUpdateOps)> {
@@ -942,7 +942,7 @@ pub fn remove_occultation(
     // that the affected pieces can know what the old situation was.
     // So we set the region to empty, and do a recalculation of the
     // relevant pieces.  Only then can we get rid of the occultation.
-    occ.region = Area::empty();
+    occ.region = Region::empty();
 
     let pieces: Vec<_> = occ.notches.iter().collect();
     for &ppiece in pieces.iter() {
index f1075d3d0c6c471932d3747a43d77ac7784de76e..4f1c17536c6d0fddd8b7d509532f2c34a9982a9d 100644 (file)
@@ -103,8 +103,8 @@ use nix::time::ClockId;
 pub const CLOCK_REALTIME : ClockId = ClockId::CLOCK_REALTIME ;
 pub const CLOCK_MONOTONIC: ClockId = ClockId::CLOCK_MONOTONIC;
 
-pub use otter_base::geometry::{Coord,Pos,PosC,Area,AreaC};
-pub use otter_base::geometry::CoordinateOverflow;
+pub use otter_base::geometry::{self,Coord,Pos,PosC,Area,AreaC};
+pub use otter_base::geometry::{CoordinateOverflow,Region};
 pub use otter_base::zcoord::{self, ZCoord};
 pub use otter_base::misc as base_misc;
 pub use base_misc::default;
index d734096f07baf1652c47442d3ba0ed0037eeb0dc..f08dba7582650302129396655eb85aacec2be3d3 100644 (file)
@@ -782,7 +782,7 @@ pub struct Rectangle { pub xy: PosC<f64> }
 
 impl Rectangle {
   #[throws(CoordinateOverflow)]
-  pub fn region(&self, centre: Pos) -> AreaC<Coord> {
+  pub fn area(&self, centre: Pos) -> AreaC<Coord> {
     let offset = (self.xy * 0.5)?;
     let offset = offset.try_map(
       |c| c.floor().to_i32().ok_or(CoordinateOverflow)
@@ -796,6 +796,11 @@ impl Rectangle {
     );
     region
   }
+
+  #[throws(CoordinateOverflow)]
+  pub fn region(&self, centre: Pos) -> Region<Coord> {
+    Region::Rectangle(self.area(centre)?)
+  }
 }
 
 #[dyn_upcast]