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) -> PolygonCreate a rectangle with one corner at origin, extending in +X and +Y.
paramoriginPointBottom-left corner of the rectangle.
paramwidthfloatWidth along the X axis.
paramheightfloatHeight along the Y axis.
Returns
Polygonfuncrect_centered(center, width, height) -> PolygonCreate a rectangle centered on a point.
paramcenterPointCenter of the rectangle.
paramwidthfloatWidth along the X axis.
paramheightfloatHeight along the Y axis.
Returns
Polygonfuncregular(center, radius, sides) -> PolygonCreate a regular polygon (equilateral triangle, square, hexagon, etc.) inscribed in a circle.
paramcenterPointCenter of the polygon.
paramradiusfloatCircumscribed radius (distance from center to each vertex).
paramsidesintNumber of sides.
Returns
PolygonMethods
func__init__(vertices) -> NoneCreate 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
Nonefuncvertices() -> list[Point]Return the list of vertices.
Returns
list[Point]funcarea() -> floatCompute the signed area of the polygon. Positive for counter-clockwise winding, negative for clockwise.
Returns
floatfunccentroid() -> PointCompute the centroid (geometric center of mass) of the polygon.
Returns
Pointfuncbbox() -> BBoxCompute the axis-aligned bounding box of the polygon.
Returns
BBoxTransformations
functranslate(v) -> PolygonReturn a new polygon translated by a vector.
paramvVector2Translation vector.
Returns
Polygonfuncrotate(angle_deg) -> PolygonRotate the polygon around the origin.
paramangle_degfloatRotation angle in degrees (counter-clockwise).
Returns
Polygonfuncrotate_around(center, angle_deg) -> PolygonRotate the polygon around an arbitrary center point.
paramcenterPointCenter of rotation.
paramangle_degfloatRotation angle in degrees (counter-clockwise).
Returns
Polygonfuncscale(sx, sy) -> PolygonScale the polygon by independent factors along each axis.
paramsxfloatScale factor along the X axis.
paramsyfloatScale factor along the Y axis.
Returns
Polygonfuncmirror_x() -> PolygonMirror the polygon across the X axis (flip Y coordinates).
Returns
Polygonfuncmirror_y() -> PolygonMirror the polygon across the Y axis (flip X coordinates).
Returns
PolygonBoolean 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 bothfuncunion(other) -> list[Polygon]Compute the union (merge) of this polygon with another. Overlapping regions are merged into a single outline.
paramotherPolygonThe 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.
paramotherPolygonThe 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.
paramotherPolygonThe 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.
paramotherPolygonThe 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) # 4Iteration: Polygons support iteration over their vertices.
for vertex in polygon:
print(vertex.x, vertex.y)