Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create constructor for ConvexPolyline that does not remove any vertices #279

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/shape/convex_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl ConvexPolygon {
/// describe a counter-clockwise convex polyline.
///
/// Convexity of the input polyline is not checked.
/// Removes some points if they are collinear with the previous one.
/// Returns `None` if all points form an almost flat line.
pub fn from_convex_polyline(mut points: Vec<Point<Real>>) -> Option<Self> {
if points.is_empty() {
Expand Down Expand Up @@ -73,6 +74,29 @@ impl ConvexPolygon {
}
}

/// Creates a new 2D convex polygon from a set of points assumed to
/// describe a counter-clockwise convex polyline.
///
/// Convexity of the input polyline is not checked.
/// Does not remove any points.
pub fn from_points(points: Vec<Point<Real>>) -> Option<Self> {
if points.is_empty() {
return None;
}
let mut normals = Vec::with_capacity(points.len());
// First, compute all normals.
for i1 in 0..points.len() {
let i2 = (i1 + 1) % points.len();
normals.push(utils::ccw_face_normal([&points[i1], &points[i2]])?);
}

if points.len() > 2 {
Some(ConvexPolygon { points, normals })
} else {
None
}
}

/// The vertices of this convex polygon.
#[inline]
pub fn points(&self) -> &[Point<Real>] {
Expand Down