diff options
Diffstat (limited to 'svgbob/src/buffer/fragment_buffer/fragment.rs')
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/svgbob/src/buffer/fragment_buffer/fragment.rs b/svgbob/src/buffer/fragment_buffer/fragment.rs index e793e19..6cc111e 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment.rs @@ -4,6 +4,13 @@ pub use arc::Arc; pub use circle::Circle; pub use line::Line; pub use marker_line::{Marker, MarkerLine}; +use ncollide2d::query::PointQuery; +use ncollide2d::{ + bounding_volume::AABB, + math::Isometry, + query::{proximity, Proximity}, + shape::{Polyline, Segment}, +}; pub use polygon::{Polygon, PolygonTag}; pub use rect::Rect; use sauron::Node; @@ -285,6 +292,37 @@ impl Fragment { } } + fn hit(&self, start: Point, end: Point) -> bool { + self.is_intersecting(AABB::new(*start, *end)) + } + + /// check if this fragment is intersecting with this bounding box + fn is_intersecting(&self, bbox: AABB<f32>) -> bool { + let bbox: Polyline<f32> = Polyline::new( + vec![ + *bbox.mins(), + *Point::new(bbox.maxs().x, bbox.mins().y), + *bbox.maxs(), + *Point::new(bbox.mins().x, bbox.maxs().y), + ], + None, + ); + let identity = Isometry::identity(); + match self { + Fragment::Line(line) => { + let segment: Segment<f32> = line.clone().into(); + proximity(&identity, &segment, &identity, &bbox, 0.0) + == Proximity::Intersecting + } + Fragment::Rect(rect) => { + let polyline: Polyline<f32> = rect.clone().into(); + proximity(&identity, &polyline, &identity, &bbox, 0.0) + == Proximity::Intersecting + } + _ => false, + } + } + /// recompute the end points of this fragment /// offset by the cell location pub fn absolute_position(&self, cell: Cell) -> Self { @@ -872,4 +910,13 @@ mod tests { lines1.sort(); assert_eq!(lines1, sorted); } + + #[test] + fn test_hit() { + let line1 = line(CellGrid::a(), CellGrid::y()); + assert!(line1.hit(CellGrid::g(), CellGrid::s())); + + let rect1 = rect(CellGrid::a(), CellGrid::y(), false, false); + assert!(!rect1.hit(CellGrid::g(), CellGrid::s())); + } } |