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
Polygons can be constructed by passing an ordered list of vertices to the
Polygon2d constructor, for example
rectangle =
Polygon2d
[ Point2d ( 1, 1 )
, Point2d ( 3, 1 )
, Point2d ( 3, 2 )
, Point2d ( 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 edges of a polygon. This will include an edge from the last point back to the first point.
Polygon2d.edges rectangle
--> [ LineSegment2d ( Point2d ( 1, 1 ), Point2d ( 3, 1 ) )
--> , LineSegment2d ( Point2d ( 3, 1 ), Point2d ( 3, 2 ) )
--> , LineSegment2d ( Point2d ( 3, 2 ), Point2d ( 1, 2 ) )
--> , LineSegment2d ( Point2d ( 1, 2 ), Point2d ( 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
--> 6Transforming a polygon is equivalent to transforming each of its vertices.
Scale a polygon about a given center point by a given scale.
point =
Point2d ( 2, 1 )
Polygon2d.scaleAbout point 2 rectangle
--> Polygon2d
--> [ Point2d ( 0, 1 )
--> , Point2d ( 4, 1 )
--> , Point2d ( 4, 3 )
--> , Point2d ( 0, 3 )
--> ]Rotate a polygon around the given center point counterclockwise by the given angle (in radians).
Polygon2d.rotateAround Point2d.origin (degrees 90) rectangle
--> Polygon2d
--> [ Point2d ( -1, 1 )
--> , Point2d ( -1, 3 )
--> , Point2d ( -2, 3 )
--> , Point2d ( -2, 1 )
--> ]Translate a polygon by the given displacement.
displacement =
Vector2d ( 2, 3 )
Polygon2d.translateBy displacement rectangle
--> Polygon2d
--> [ Point2d ( 3, 4 )
--> , Point2d ( 5, 4 )
--> , Point2d ( 5, 5 )
--> , Point2d ( 3, 5 )
--> ]Mirror a polygon across the given axis.
Polygon2d.mirrorAcross Axis2d.x rectangle
--> Polygon2d
--> [ Point2d ( 1, -1 )
--> , Point2d ( 3, -1 )
--> , Point2d ( 3, -2 )
--> , Point2d ( 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 map, for example
Polygon2d.mirrorAcross Axis2d.x polygonis equivalent to
Polygon2d.map (Point2d.mirrorAcross Axis2d.x) polygonTake a polygon defined in global coordinates, and return it expressed in local coordinates relative to a given reference frame.
localFrame =
Frame2d.at (Point2d ( 1, 2 ))
Polygon2d.relativeTo localFrame rectangle
--> Polygon2d
--> [ Point2d ( 0, -1 )
--> , Point2d ( 2, -1 )
--> , Point2d ( 2, 0 )
--> , Point2d ( 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.at (Point2d ( 1, 2 ))
Polygon2d.placeIn localFrame rectangle
--> Polygon2d
--> [ Point2d ( 2, 3 )
--> , Point2d ( 4, 3 )
--> , Point2d ( 4, 4 )
--> , Point2d ( 2, 4 )
--> ]Get the minimal bounding box containing a given polygon. Returns Nothing
if the polygon has no vertices.
Polygon2d.boundingBox rectangle
--> Just
--> (BoundingBox2d
--> { minX = 1
--> , maxX = 3
--> , minY = 1
--> , maxY = 2
--> }
--> )
Get the vertices of a polygon.