grains.geometry.Polygon

class grains.geometry.Polygon(vertices)[source]

Represents a polygon.

This class works as expected as long as the given polygon is simple, i.e. it is not self-intersecting and does not contain holes.

A simple class that has numpy and matplotlib (for the visualization) as the only dependencies. It does not want to provide extensive functionalities (for those, check out Shapely). The polygon is represented by its vertices, given in a consecutive order.

Parameters

vertices (ndarray) – 2D numpy array with 2 columns, each row corresponding to a vertex, and the two columns giving the Cartesian coordinates of the vertex.

Raises
  • Exception – If all the vertices of the polygon lie along the same line. If the polygon is not given in R^2.

  • ValueError – If the polygon does not have at least 3 vertices.

Examples

Try to give a “polygon”, in which all vertices are collinear

>>> poly = Polygon(np.array([[0, 0], [1, 1], [2, 2]]))  
Traceback (most recent call last):
...
Exception: All vertices are collinear. Not a valid polygon.

Now we give a valid polygon:

>>> pentagon = Polygon(np.array([[2, 1], [0, 0], [0.5, 3], [-1, 4], [3, 5]]))

Use Python’s print function to display basic information about a polygon:

>>> print(pentagon)
A non-convex polygon with 5 vertices, oriented clockwise.
__init__(vertices)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(vertices)

Initialize self.

area()

Signed area of the polygon.

centroid()

Centroid of the polygon.

diameter([definition])

Diameter of the polygon.

is_convex()

Decides whether the polygon is convex.

orientation()

Orientation of the polygon.

plot(*args, **kwargs)

Plots the polygon.

Attributes

plot_options