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

Boolean operations

Boolean operations combine or cut polygons. All four methods return a list[Polygon] because a single operation can produce multiple disjoint pieces. Holes created by subtraction are handled via keyholing (a zero-width bridge connecting the hole to the exterior), keeping every result a single-ring polygon compatible with GDS-II.

a = Polygon.rect(Point(0, 0), 10, 10)
b = Polygon.rect(Point(5, 0), 10, 10)

merged = a.union(b)        # combined outline
diff   = a.subtract(b)     # a minus overlap
common = a.intersect(b)    # overlap only
sym    = a.xor(b)          # either but not both
funcunion(other) -> list[Polygon]

Compute the union (merge) of this polygon with another. Overlapping regions are merged into a single outline.

paramotherPolygon

The polygon to union with.

Returns

list[Polygon]
funcsubtract(other) -> list[Polygon]

Subtract another polygon from this one. Returns the area of this polygon that does not overlap with other. If other is fully contained, the result is a keyholed polygon with a zero-width bridge.

paramotherPolygon

The polygon to subtract.

Returns

list[Polygon]
funcintersect(other) -> list[Polygon]

Compute the intersection of this polygon with another. Returns the overlapping area shared by both inputs. Empty list if no overlap.

paramotherPolygon

The polygon to intersect with.

Returns

list[Polygon]
funcxor(other) -> list[Polygon]

Compute the symmetric difference (XOR) of this polygon with another. Returns the area in either polygon but not both. Empty list if polygons are identical.

paramotherPolygon

The polygon to XOR with.

Returns

list[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