pub trait TriangulateEarcut<T: CoordFloat> {
// Required method
fn earcut_triangles_raw(&self) -> RawTriangulation<T>;
// Provided methods
fn earcut_triangles(&self) -> Vec<Triangle<T>> { ... }
fn earcut_triangles_iter(&self) -> Iter<T> ⓘ { ... }
}
Expand description
Triangulate polygons using an ear-cutting algorithm.
Requires the "earcutr"
feature, which is enabled by default.
Required Methods§
Sourcefn earcut_triangles_raw(&self) -> RawTriangulation<T>
fn earcut_triangles_raw(&self) -> RawTriangulation<T>
Return the raw result from the earcutr
library: a one-dimensional vector of polygon
vertices (in XY order), and the indices of the triangles within the vertices vector. This
method is less ergonomic than the earcut_triangles
and earcut_triangles_iter
methods, but can be helpful when working in graphics contexts that expect flat vectors of
data.
§Examples
use geo::{coord, polygon, Triangle, TriangulateEarcut};
use geo::triangulate_earcut::RawTriangulation;
let square_polygon = polygon![
(x: 0., y: 0.), // SW
(x: 10., y: 0.), // SE
(x: 10., y: 10.), // NE
(x: 0., y: 10.), // NW
(x: 0., y: 0.), // SW
];
let mut triangles_raw = square_polygon.earcut_triangles_raw();
assert_eq!(
RawTriangulation {
vertices: vec![
0., 0., // SW
10., 0., // SE
10., 10., // NE
0., 10., // NW
0., 0., // SW
],
triangle_indices: vec![
3, 0, 1, // NW-SW-SE
1, 2, 3, // SE-NE-NW
],
},
triangles_raw,
);
Provided Methods§
Sourcefn earcut_triangles(&self) -> Vec<Triangle<T>>
fn earcut_triangles(&self) -> Vec<Triangle<T>>
§Examples
use geo::{coord, polygon, Triangle, TriangulateEarcut};
let square_polygon = polygon![
(x: 0., y: 0.), // SW
(x: 10., y: 0.), // SE
(x: 10., y: 10.), // NE
(x: 0., y: 10.), // NW
(x: 0., y: 0.), // SW
];
let triangles = square_polygon.earcut_triangles();
assert_eq!(
vec![
Triangle(
coord! { x: 0., y: 10. }, // NW
coord! { x: 10., y: 10. }, // NE
coord! { x: 10., y: 0. }, // SE
),
Triangle(
coord! { x: 10., y: 0. }, // SE
coord! { x: 0., y: 0. }, // SW
coord! { x: 0., y: 10. }, // NW
),
],
triangles,
);
Sourcefn earcut_triangles_iter(&self) -> Iter<T> ⓘ
fn earcut_triangles_iter(&self) -> Iter<T> ⓘ
§Examples
use geo::{coord, polygon, Triangle, TriangulateEarcut};
let square_polygon = polygon![
(x: 0., y: 0.), // SW
(x: 10., y: 0.), // SE
(x: 10., y: 10.), // NE
(x: 0., y: 10.), // NW
(x: 0., y: 0.), // SW
];
let mut triangles_iter = square_polygon.earcut_triangles_iter();
assert_eq!(
Some(Triangle(
coord! { x: 0., y: 10. }, // NW
coord! { x: 10., y: 10. }, // NE
coord! { x: 10., y: 0. }, // SE
)),
triangles_iter.next(),
);
assert_eq!(
Some(Triangle(
coord! { x: 10., y: 0. }, // SE
coord! { x: 0., y: 0. }, // SW
coord! { x: 0., y: 10. }, // NW
)),
triangles_iter.next(),
);
assert!(triangles_iter.next().is_none());