Polygon

A closed polygon defined by a list of vertices.

Polygons are the primary geometry primitive for layout. They represent filled shapes on a layer -- waveguides, pads, gratings, and any other structure. Polygons are immutable; all transformation methods return new Polygon instances.

# Rectangle from origin
rect = Polygon.rect(Point.origin(), 10, 5)

# Centered rectangle
square = Polygon.rect_centered(Point(50, 50), 20, 20)

# Regular hexagon
hex = Polygon.regular(Point(0, 0), radius=10, sides=6)

# Custom shape
tri = Polygon([Point(0, 0), Point(10, 0), Point(5, 8)])

Static constructors

funcrect(origin, width, height) -> Polygon

Create a rectangle with one corner at origin, extending in +X and +Y.

paramoriginPoint

Bottom-left corner of the rectangle.

paramwidthfloat

Width along the X axis.

paramheightfloat

Height along the Y axis.

Returns

Polygon
funcrect_centered(center, width, height) -> Polygon

Create a rectangle centered on a point.

paramcenterPoint

Center of the rectangle.

paramwidthfloat

Width along the X axis.

paramheightfloat

Height along the Y axis.

Returns

Polygon
funcregular(center, radius, sides) -> Polygon

Create a regular polygon (equilateral triangle, square, hexagon, etc.) inscribed in a circle.

paramcenterPoint

Center of the polygon.

paramradiusfloat

Circumscribed radius (distance from center to each vertex).

paramsidesint

Number of sides.

Returns

Polygon

Methods

func__init__(vertices) -> None

Create a polygon from a list of vertices. The polygon is automatically closed (you do not need to repeat the first vertex).

paramverticeslist[Point]

Ordered list of vertex points.

Returns

None
funcvertices() -> list[Point]

Return the list of vertices.

Returns

list[Point]
funcarea() -> float

Compute the signed area of the polygon. Positive for counter-clockwise winding, negative for clockwise.

Returns

float
funccentroid() -> Point

Compute the centroid (geometric center of mass) of the polygon.

Returns

Point
funcbbox() -> BBox

Compute the axis-aligned bounding box of the polygon.

Returns

BBox

Transformations

functranslate(v) -> Polygon

Return a new polygon translated by a vector.

paramvVector2

Translation vector.

Returns

Polygon
funcrotate(angle_deg) -> Polygon

Rotate the polygon around the origin.

paramangle_degfloat

Rotation angle in degrees (counter-clockwise).

Returns

Polygon
funcrotate_around(center, angle_deg) -> Polygon

Rotate the polygon around an arbitrary center point.

paramcenterPoint

Center of rotation.

paramangle_degfloat

Rotation angle in degrees (counter-clockwise).

Returns

Polygon
funcscale(sx, sy) -> Polygon

Scale the polygon by independent factors along each axis.

paramsxfloat

Scale factor along the X axis.

paramsyfloat

Scale factor along the Y axis.

Returns

Polygon
funcmirror_x() -> Polygon

Mirror the polygon across the X axis (flip Y coordinates).

Returns

Polygon
funcmirror_y() -> Polygon

Mirror the polygon across the Y axis (flip X coordinates).

Returns

Polygon

Supported operations

Length: len(polygon) returns the number of vertices.

rect = Polygon.rect(Point.origin(), 10, 5)
len(rect)  # 4

Iteration: Polygons support iteration over their vertices.

for vertex in polygon:
    print(vertex.x, vertex.y)

On this page