8个版本
0.4.2 | 2021年12月14日 |
---|---|
0.4.1 | 2021年7月15日 |
0.4.0 | 2021年4月26日 |
0.3.1 | 2021年2月28日 |
0.1.2 | 2021年2月7日 |
#1841 在 算法
105KB
2K SLoC
intersect2d
在观看Philipp Kindermann的优秀sweep-line视频后,我认为我最终理解了这个算法的工作原理。
这是我关于线段交点sweep-line算法实现的一个简陋看法。
库crate还包含一个线段交点函数。
这个crate是教育性的,不适合任何生产用途。它已经被cgmath库这里采用。
交互式分步示例
cargo run --example fltk_gui --features console_trace
交点函数API示例
use intersect2d::{intersect, Intersection};
let line1 = geo::Line::<f64>::from([(100.0,150.),(150.0,100.)]);
let line2 = geo::Line::<f64>::from([(100.0,150.),(150.0,100.)]);
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
println!("{:?}", rv.unwrap().single());
Sweep-line API示例
let lines = vec![
geo::Line::<f64>::from([(200.0,200.),(350.0,300.)]),
geo::Line::<f64>::from([(400.0,200.),(250.0,300.)]),
];
let results = intersect2d::algorithm::AlgorithmData::<f64>::default()
.with_ignore_end_point_intersections(false)?
.with_lines(lines.into_iter())?
.compute()?;
for (point, line) in results {
println!("Intersection @{:?} Involved lines:{:?}", point, line);
}
检测geo::LineString的自身交点
let coordinates = vec![(200., 200.), (300., 300.), (400., 200.), (200., 300.)];
let line_string = geo::LineString::<f32>::from(coordinates);
// 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(nlog(n)+k)
// sweep-line algorithm if n is small enough.
let result = intersect2d::algorithm::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 {
println!("Intersection detected @{:?} Involved lines:{:?}", p, l);
}
或使用SelfIntersectingExclusive
特质
// SelfIntersectingExclusive does not report endpoint intersections
use intersect2d::SelfIntersectingExclusive;
let coordinates = vec![(200., 200.), (300., 300.), (400., 200.), (200., 300.)];
let line_string = geo::LineString::from(coordinates);
if line_string.is_self_intersecting()? {
println!("Intersection detected");
}
for intersections in line_string.self_intersections()? {
println!("Intersection: {:?}", intersections);
}
您还可以使用geo::Line
和SelfIntersectingInclusive
特质检查一组geo::Line
的自身交点
// SelfIntersectingInclusive reports endpoint intersections
use intersect2d::SelfIntersectingInclusive;
let lines = vec![
geo::Line::<f64>::from([(200.0,200.),(350.0,300.)]),
geo::Line::<f64>::from([(400.0,200.),(250.0,300.)]),
];
if lines.is_self_intersecting_inclusive()? {
println!("Intersection detected");
}
for intersections in lines.self_intersections_inclusive()? {
println!("Intersection: {:?}", intersections);
}
待办事项
- 基准测试和优化
- 稳定的共线线重叠检测
依赖
~5MB
~98K SLoC