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
PolygonSupported 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)