From: Ian Jackson Date: Thu, 25 Mar 2021 01:43:59 +0000 (+0000) Subject: Provide distinction betwene Region and Area X-Git-Tag: otter-0.5.0~429 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8870d6805b3fe74d51e5284f25b81612c10f584b;p=otter.git Provide distinction betwene Region and Area The latter will always be rectangle. Signed-off-by: Ian Jackson --- diff --git a/base/geometry.rs b/base/geometry.rs index 2e1708ce..164b0fe7 100644 --- a/base/geometry.rs +++ b/base/geometry.rs @@ -251,4 +251,16 @@ impl Region { Rectangle(a) => a.contains(pos), } } + + pub fn overlaps(&self, other: &Region) -> 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()) + } + } diff --git a/src/hand.rs b/src/hand.rs index 5db54ad3..a6c2b7a6 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -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, diff --git a/src/hidden.rs b/src/hidden.rs index 289e3c00..e66643a9 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -43,7 +43,7 @@ struct Passive { #[derive(Clone,Debug,Serialize,Deserialize)] pub struct Occultation { - region: Area, // automatically affect pieces here + region: Region, // 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, 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() { diff --git a/src/prelude.rs b/src/prelude.rs index f1075d3d..4f1c1753 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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; diff --git a/src/shapelib.rs b/src/shapelib.rs index d734096f..f08dba75 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -782,7 +782,7 @@ pub struct Rectangle { pub xy: PosC } impl Rectangle { #[throws(CoordinateOverflow)] - pub fn region(&self, centre: Pos) -> AreaC { + pub fn area(&self, centre: Pos) -> AreaC { 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 { + Region::Rectangle(self.area(centre)?) + } } #[dyn_upcast]