A Polygon2d represents a closed polygon in 2D, and is defined by a list of
vertices. This module contains a variety of polygon-related functionality, such
as
Construct a polygon from a list of its vertices:
rectangle =
Polygon2d.fromVertices
[ Point2d.fromCoordinates ( 1, 1 )
, Point2d.fromCoordinates ( 3, 1 )
, Point2d.fromCoordinates ( 3, 2 )
, Point2d.fromCoordinates ( 1, 2 )
]The last vertex is implicitly considered to be connected back to the first vertex (you do not have to close the polygon explicitly).
Get the vertices of a polygon.
Polygon2d.vertices rectangle
--> [ Point2d.fromCoordinates ( 1, 1 )
--> , Point2d.fromCoordinates ( 3, 1 )
--> , Point2d.fromCoordinates ( 3, 2 )
--> , Point2d.fromCoordinates ( 1, 2 )
--> ]Get the edges of a polygon. This will include an edge from the last point back to the first point.
Polygon2d.edges rectangle
--> [ LineSegment2d.fromEndpoints
--> ( Point2d.fromCoordinates ( 1, 1 )
--> , Point2d.fromCoordinates ( 3, 1 )
--> )
--> , LineSegment2d.fromEndpoints
--> ( Point2d.fromCoordinates ( 3, 1 )
--> , Point2d.fromCoordinates ( 3, 2 )
--> )
--> , LineSegment2d.fromEndpoints
--> ( Point2d.fromCoordinates ( 3, 2 )
--> , Point2d.fromCoordinates ( 1, 2 )
--> )
--> , LineSegment2d.fromEndpoints
--> ( Point2d.fromCoordinates ( 1, 2 )
--> , Point2d.fromCoordinates ( 1, 1 )
--> )
--> ]Get the perimeter of a polygon (the sum of the lengths of its edges).
Polygon2d.perimeter rectangle
--> 6Get the area of a polygon. This value will never be negative.
Polygon2d.area rectangle
--> 2Get the signed area of a polygon, with polygons with vertices in clockwise order considered to have positive area and polygons with vertices in counterclockwise order considered to have negative area.
Polygon2d.clockwiseArea rectangle
--> -6Get the signed area of a polygon, with polygons with vertices in counterclockwise order considered to have positive area and polygons with vertices in clockwise order considered to have negative area.
Polygon2d.counterclockwiseArea rectangle
--> 6Get the minimal bounding box containing a given polygon. Returns Nothing
if the polygon has no vertices.
Polygon2d.boundingBox rectangle
--> Just
--> (BoundingBox2d.with
--> { minX = 1
--> , maxX = 3
--> , minY = 1
--> , maxY = 2
--> }
--> )Transforming a polygon is equivalent to transforming each of its vertices.
Scale a polygon about a given center point by a given scale.
point =
Point2d.fromCoordinates ( 2, 1 )
Polygon2d.scaleAbout point 2 rectangle
--> Polygon2d.fromVertices
--> [ Point2d.fromCoordinates ( 0, 1 )
--> , Point2d.fromCoordinates ( 4, 1 )
--> , Point2d.fromCoordinates ( 4, 3 )
--> , Point2d.fromCoordinates ( 0, 3 )
--> ]Rotate a polygon around the given center point counterclockwise by the given angle (in radians).
rectangle
|> Polygon2d.rotateAround Point2d.origin
(degrees 90)
--> Polygon2d.fromVertices
--> [ Point2d.fromCoordinates ( -1, 1 )
--> , Point2d.fromCoordinates ( -1, 3 )
--> , Point2d.fromCoordinates ( -2, 3 )
--> , Point2d.fromCoordinates ( -2, 1 )
--> ]Translate a polygon by the given displacement.
displacement =
Vector2d.fromComponents ( 2, 3 )
Polygon2d.translateBy displacement rectangle
--> Polygon2d.fromVertices
--> [ Point2d.fromCoordinates ( 3, 4 )
--> , Point2d.fromCoordinates ( 5, 4 )
--> , Point2d.fromCoordinates ( 5, 5 )
--> , Point2d.fromCoordinates ( 3, 5 )
--> ]Mirror a polygon across the given axis.
Polygon2d.mirrorAcross Axis2d.x rectangle
--> Polygon2d.fromVertices
--> [ Point2d.fromCoordinates ( 1, -1 )
--> , Point2d.fromCoordinates ( 3, -1 )
--> , Point2d.fromCoordinates ( 3, -2 )
--> , Point2d.fromCoordinates ( 1, -2 )
--> ]Note that if a polygon's vertices were in counterclockwise order before mirroring, they will be in clockwise order afterward, and vice versa.
Transform each vertex of a polygon by the given function. All other
transformations can be defined in terms of mapVertices; for example,
Polygon2d.mirrorAcross axisis equivalent to
Polygon2d.mapVertices (Point2d.mirrorAcross axis)Take a polygon defined in global coordinates, and return it expressed in local coordinates relative to a given reference frame.
localFrame =
Frame2d.atPoint (Point2d.fromCoordinates ( 1, 2 ))
Polygon2d.relativeTo localFrame rectangle
--> Polygon2d.fromVertices
--> [ Point2d.fromCoordinates ( 0, -1 )
--> , Point2d.fromCoordinates ( 2, -1 )
--> , Point2d.fromCoordinates ( 2, 0 )
--> , Point2d.fromCoordinates ( 0, 0 )
--> ]Take a polygon considered to be defined in local coordinates relative to a given reference frame, and return that polygon expressed in global coordinates.
localFrame =
Frame2d.atPoint (Point2d.fromCoordinates ( 1, 2 ))
Polygon2d.placeIn localFrame rectangle
--> Polygon2d.fromVertices
--> [ Point2d.fromCoordinates ( 2, 3 )
--> , Point2d.fromCoordinates ( 4, 3 )
--> , Point2d.fromCoordinates ( 4, 4 )
--> , Point2d.fromCoordinates ( 2, 4 )
--> ]