Skip to main content

Polygon

Struct Polygon 

Source
pub struct Polygon {
    pub vertices: Vec<Point>,
}

Fields§

§vertices: Vec<Point>

Implementations§

Source§

impl Polygon

Source

pub fn new(vertices: Vec<Point>) -> Self

Create a polygon from a list of vertices.

Consecutive duplicate vertices and a trailing vertex that closes back on the first are removed automatically.

§Panics

Panics if:

  • fewer than 3 vertices are provided,
  • any vertex has a non-finite x or y,
  • fewer than 3 distinct vertices remain after deduplication,
  • the polygon has (near-)zero signed area (degenerate).

The area check uses a numerically stable vertex-reference formula so that polygons far from the origin are not spuriously rejected.

Source

pub fn offset(&self, distance: f64) -> Option<Polygon>

Offset the polygon by distance using mitred corners.

Positive distances grow a CCW polygon outward; negative distances shrink it. Returns None if the offset polygon degenerates (e.g. an inward offset larger than the polygon size).

Mirrors shapely’s buffer(distance, join_style=mitre) used by Python sectionproperties’ Geometry.offset().

Source

pub fn offset_with_style( &self, distance: f64, join_style: JoinStyle, mitre_limit: f64, ) -> Option<Polygon>

Offset the polygon with explicit join style and mitre limit.

join_style controls corner treatment:

  • Miter: Extend offset edges until they intersect (default)
  • Round: Arc of radius |distance| (approximated with segments)
  • Bevel: Connect offset edges with a single segment

mitre_limit (only for Miter) caps the mitre length to mitre_limit * |distance|. Exceeding corners are bevelled instead. Typical value: 2.0.

Source

pub fn signed_area(&self) -> f64

Calculate the signed area of the polygon.

Counter-clockwise polygon -> positive Clockwise polygon -> negative

Source

pub fn area(&self) -> f64

Calculate the absolute area of the polygon.

Source

pub fn centroid(&self) -> Point

Calculate the centroid of the polygon.

Source

pub fn moment_of_inertia_x(&self) -> f64

Calculate the second moment of area about the global x-axis.

Source

pub fn moment_of_inertia_y(&self) -> f64

Calculate the second moment of area about the global y-axis.

Source

pub fn product_of_inertia_xy(&self) -> f64

Calculate the product of area about the global axes.

Source

pub fn centroidal_moment_of_inertia_x(&self) -> f64

Calculate the second moment of area about the centroidal x-axis.

Uses a vertex-reference two-pass strategy: the first vertex is used as a reference point so that all shifted coordinates are small (order of polygon extent). The centroid offset is computed in these shifted coordinates, then the moment integral is evaluated about the centroid. This avoids the catastrophic cancellation that occurs in Ix_global - A * y_c² when the polygon is far from the origin.

Source

pub fn centroidal_moment_of_inertia_y(&self) -> f64

Calculate the second moment of area about the centroidal y-axis.

Uses the same vertex-reference two-pass strategy as centroidal_moment_of_inertia_x.

Source

pub fn centroidal_product_of_inertia_xy(&self) -> f64

Calculate the product of area about the centroidal axes.

Uses the same vertex-reference two-pass strategy as centroidal_moment_of_inertia_x.

Source

pub fn rotate(&self, angle: f64) -> Self

Rotate all vertices about the origin by angle radians (CCW positive).

Source

pub fn contains_point(&self, point: Point) -> bool

Check if a point is inside the polygon (ray casting algorithm).

Source

pub fn perimeter(&self) -> f64

Calculate the perimeter of the polygon.

Source

pub fn max_fiber_distance_y(&self) -> f64

Maximum fiber distance from centroid in Y direction (for section modulus).

Source

pub fn max_fiber_distance_x(&self) -> f64

Maximum fiber distance from centroid in X direction (for section modulus).

Source

pub fn clip_halfspace( &self, n_x: f64, n_y: f64, c: f64, below: bool, ) -> Option<Polygon>

Clip the polygon against the half-plane nx*x + ny*y <= c (below = true) or >= c (below = false), using Sutherland-Hodgman clipping. Returns None when nothing remains.

Source

pub fn split_by_line( &self, a: Point, b: Point, ) -> (Option<Polygon>, Option<Polygon>)

Split the polygon into the two halves lying either side of the line through points a and b.

Returns (below, above); either side may be None when the line does not cut the polygon. Mirrors Python bisect_section helpers used by Section.split_section.

Source

pub fn has_self_intersections(&self) -> bool

Check if the polygon has self-intersections (non-adjacent edges crossing).

Returns true if any two non-adjacent edges intersect in their interiors. This does not count shared vertices (adjacent edges) as intersections.

Source

pub fn try_new(vertices: Vec<Point>) -> Result<Self, PolygonError>

Validating constructor: returns an error if the polygon has self-intersections or is otherwise invalid.

Checks:

  • At least 3 distinct vertices
  • All vertices are finite
  • Non-zero area
  • No self-intersections

Trait Implementations§

Source§

impl BoundaryExtrema for Polygon

Source§

fn extreme_distances(&self, center: Point, direction: Point) -> (f64, f64)

Signed distances (min, max) from center projected onto direction. Read more
Source§

fn extreme_y(&self, center: Point) -> (f64, f64)

Convenience: extreme distances along the global Y axis.
Source§

fn extreme_x(&self, center: Point) -> (f64, f64)

Convenience: extreme distances along the global X axis.
Source§

impl Clone for Polygon

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Polygon

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.