5 unstable releases
new 0.3.1 | Feb 28, 2021 |
---|---|
0.3.0 | Feb 22, 2021 |
0.2.0 | Feb 8, 2021 |
0.1.2 | Feb 7, 2021 |
#113 in Algorithms
47 downloads per month
Used in centerline
84KB
1.5K
SLoC
intersect2d
After watching Philipp Kindermann's excellent sweep-line videos I think I finally understand how this algorithm works.
This is my humble take on an implementation of the segment line
intersection sweep-line algorithm.
The library crate also contains a line intersection function.
Code still in development, not ready for any purpose.
Interactive step-by-step example:
cargo run --example fltk_gui --features console_trace
Most of this crate have been adapted for nalgebra,
cgmath, mint and plain vector here.
This crate will remain geo based.
Intersection function API example:
use intersection2d::{intersect, Intersection};
use geo;
let line1:geo::Line::<f64> = [(100.0,150.),(150.0,100.)].into();
let line2:geo::Line::<f64> = [(100.0,150.),(150.0,100.)].into();
let _rv = intersect(&line1, &line2);
match _rv {
Some(Intersection::Intersection(_a)) => panic!("expected an overlap"),
Some(Intersection::OverLap(a)) => println!("{:?}", a),
None => panic!("expected an overlap"),
}
// you can also get a single intersection point from the Intersection enum.
// Albeit geometrically incorrect, it makes things easy
if let Some(_rv) =_rv {
println!("{:?}", _rv.single());
}
Sweep-line API example:
use geo;
use intersect2d::algorithm::AlgorithmData;
let _l = vec![
geo::Line::new(
geo::Coordinate { x: 200., y: 200. },
geo::Coordinate { x: 350., y: 300. },
),
geo::Line::new(
geo::Coordinate { x: 400., y: 200. },
geo::Coordinate { x: 250., y: 300. },
),
];
let results = AlgorithmData::<f64>::default()
.with_ignore_end_point_intersections(false)?
.with_lines(_l.into_iter())?
.compute()?;
for (p, l) in results.iter() {
println!("Intersection @{:?} Involved lines:{:?}", p, l);
}
Detection of self-intersecting geo::LineString:
let coords = vec![(200., 200.), (300., 300.), (400., 200.), (200., 300.)];
let line_string: geo::LineString<f32> = coords.into_iter().collect();
// Obviously this example only makes sense for LinesStrings with many points.
// A simple brute force O(n²) intersection test will be faster than this O(log(n)n)
// sweep-line algorithm if n is small enough.
let result = AlgorithmData::<f32>::default()
.with_ignore_end_point_intersections(true)?
.with_stop_at_first_intersection(true)?
.with_lines(line_string.lines())?
.compute()?;
for (p, l) in result.iter() {
println!("Intersection detected @{:?} Involved lines:{:?}", p, l);
}
Todo
- Error handling
- Benchmark and optimize
- Stable overlapping co-linear line detection
Dependencies
~3.5MB
~70K SLoC