NFP - No Fit Polygon
A Rust library that computes the No Fit Polygon (Minkowski sum) of two closed counter-clockwise oriented polygons.
Overview
NFP implements the Minkowski sum algorithm for polygon nesting and packing problems. Given two CCW-oriented polygons, it computes the boundary region where one polygon can be placed relative to another without overlapping.
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Usage
use *;
// Define two CCW-oriented polygons using the point() shortcut
let poly_a = vec!;
let poly_b = vec!;
// Calculate the No Fit Polygon
match NFPnfp
Requirements
- Both polygons must be closed (first and last points form an edge)
- Both polygons must be oriented counter-clockwise (CCW)
- Both polygons must have at least 3 vertices
API
point(x, y)
Shortcut function to create a new point.
Point
2D point with x, y coordinates.
Methods:
new(x, y)- Create a new pointdistance(other)- Calculate distance to another pointdistance_squared(other)- Calculate squared distanceadd(other)- Vector additionsub(other)- Vector subtraction
NfpError
Error type for NFP operations.
Variants:
EmptyPolygon- One or both input polygons are emptyInsufficientVertices- One or both polygons have fewer than 3 vertices
Implements Display and std::error::Error traits. You can simply print errors with {} formatting or use the enum variants for pattern matching when you need programmatic error handling.
NFP
Main calculator for Minkowski sums.
Methods:
nfp(poly_a, poly_b) -> Result<Vec<Point>, NfpError>- Compute NFP of two polygons
polygon module
Utility functions for polygon operations:
len(vertices)- Get vertex countis_empty(vertices)- Check if emptyis_ccw(vertices)- Check counter-clockwise orientationensure_ccw(vertices)- Ensure CCW orientation (mutates)translate(vertices, offset)- Translate polygon by offset
Related Projects
NFP is part of the open-sourced Nest2D projects collection.