RTree can store Lines. A Line consists of two points.
let line1 = Line::new((0., 0.), (0., 2.));The code above constructs a line starting from (0., 0.) and ending at (0., 2.).
Here is the complete example:
use rstar::{primitives::Line, RTree};
fn main() {
let line1 = Line::new((0., 0.), (0., 2.));
let line2 = Line::new((1., 0.), (3., 0.));
let line3 = Line::new((2., 0.), (0., 2.));
let tree = RTree::bulk_load(vec![line1, line2, line3]);
for li in tree.nearest_neighbor_iter_with_distance_2(&(1., 1.)) {
println!("{:?}", li);
}
}Output:
(Line { from: (2.0, 0.0), to: (0.0, 2.0) }, 0.0)
(Line { from: (1.0, 0.0), to: (3.0, 0.0) }, 1.0)
(Line { from: (0.0, 0.0), to: (0.0, 2.0) }, 1.0)
The example above lists all the lines sorted by their distance to point (1., 1.).
The distance is computed by nearest_point.
We can obtain the length of a line by length_2, which returns the distance between the line's from point and to point.
Similar to points, lines can be constructed in high dimensions.
➡️ Next: Rectangles
📘 Back: Table of contents