Skip to main content

offset_polygon_2d

Function offset_polygon_2d 

Source
pub fn offset_polygon_2d(
    vertices: &[Point2],
    distance: f64,
    tolerance: f64,
) -> Result<Vec<Point2>, MathError>
Expand description

Offset a closed 2D polygon by a signed distance.

Positive distance offsets outward (assuming CCW winding), negative offsets inward. Applies a miter limit of 2× the offset distance to prevent spikes at sharp corners, and removes self-intersections from the result.

§Algorithm

For each edge, compute a parallel offset line by translating along the outward normal. Intersect adjacent offset lines to find new vertex positions (miter join). Degenerate intersections (parallel edges) fall back to simple translation.

§Errors

Returns an error if fewer than 3 vertices are provided.