A Polygon2d represents a closed polygon in 2D, optionally with holes. It
is defined by an outer loop of vertices and a list of inner loops defining any
holes. This module contains a variety of polygon-related functionality, such as
Construct a polygon without holes from a list of its vertices:
rectangle =
Polygon2d.singleLoop
[ Point2d.meters 1 1
, Point2d.meters 3 1
, Point2d.meters 3 2
, Point2d.meters 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). Vertices should ideally be provided in counterclockwise order; if they are provided in clockwise order they will be reversed.
Construct a polygon with holes from one outer loop and a list of inner loops. The loops must not touch or intersect each other.
outerLoop =
[ Point2d.meters 0 0
, Point2d.meters 3 0
, Point2d.meters 3 3
, Point2d.meters 0 3
]
innerLoop =
[ Point2d.meters 1 1
, Point2d.meters 1 2
, Point2d.meters 2 2
, Point2d.meters 2 1
]
squareWithHole =
Polygon2d.withHoles [ innerLoop ] outerLoopAs with Polygon2d.singleLoop, the last vertex of each loop is considered to be
connected back to the first. Vertices of the outer loop should ideally be
provided in counterclockwise order and vertices of the inner loops should
ideally be provided in clockwise order.
Constructs a regular convex polygon with the specified center point, circumradius and number of sides. The polygon will be oriented so that the bottom edge is horizontal (assuming a Y-up coordinate system; note that SVG uses Y-down instead!).
Get the list of vertices definining the outer loop (border) of a polygon. The vertices will be in counterclockwise order.
Polygon2d.outerLoop squareWithHole
--> [ Point2d.meters 0 0
--> , Point2d.meters 3 0
--> , Point2d.meters 3 3
--> , Point2d.meters 0 3
--> ]Get the holes (if any) of a polygon, each defined by a list of vertices. Each list of vertices will be in clockwise order.
Polygon2d.innerLoops squareWithHole
--> [ [ Point2d.meters 1 1
--> , Point2d.meters 1 2
--> , Point2d.meters 2 2
--> , Point2d.meters 2 1
--> ]
--> ]Get all vertices of a polygon; this will include vertices from the outer loop of the polygon and all inner loops. The order of the returned vertices is undefined.
Get all edges of a polygon. This will include both outer edges and inner (hole) edges.
Get the perimeter of a polygon (the sum of the lengths of its edges). This includes the outer perimeter and the perimeter of any holes.
Polygon2d.perimeter squareWithHole
--> Length.meters 16Get the area of a polygon. This value will never be negative.
Polygon2d.area squareWithHole
--> Area.squareMeters 8Get the centroid of a polygon. Returns Nothing if the polygon has no
vertices or zero area.
Get the minimal bounding box containing a given polygon. Returns Nothing
if the polygon has no vertices.
Polygon2d.boundingBox rectangle
--> Just <|
--> BoundingBox2d.from
--> (Point2d.meters 1 1)
--> (Point2d.meters 3 2)Check if a polygon contains a given point.
This is an O(n) operation. The polygon can have holes and does not need to be convex.
These transformations generally behave just like the ones in the Point2d
module.
Scale a polygon about a given center point by a given scale. If the given scale is negative, the order of the polygon's vertices will be reversed so that the resulting polygon still has its outer vertices in counterclockwise order and its inner vertices in clockwise order.
Rotate a polygon around the given center point counterclockwise by the given angle.
Translate a polygon by the given displacement.
Translate a polygon in a given direction by a given distance.
Mirror a polygon across the given axis. The order of the polygon's vertices will be reversed so that the resulting polygon still has its outer vertices in counterclockwise order and its inner vertices in clockwise order.
Convert a polygon from one units type to another, by providing a conversion factor given as a rate of change of destination units with respect to source units.
Convert a polygon from one units type to another, by providing an 'inverse' conversion factor given as a rate of change of source units with respect to destination units.
Take a polygon defined in global coordinates, and return it expressed in local coordinates relative to a given reference frame. If the given frame is left-handed, the order of the polygon's vertices will be reversed so that the resulting polygon still has its outer vertices in counterclockwise order and its inner vertices in clockwise order.
Take a polygon considered to be defined in local coordinates relative to a given reference frame, and return that polygon expressed in global coordinates. If the given frame is left-handed, the order of the polygon's vertices will be reversed so that the resulting polygon still has its outer vertices in counterclockwise order and its inner vertices in clockwise order.
Triangulate a polygon. This uses the TriangularMesh data types from
ianmackenzie/elm-triangular-mesh.
Triangulation is useful for things like WebGL rendering; you can define a
polygon just by specifying its outline (and holes, if it has any)
then use this function to turn that polygon into a list of triangles which you can render using WebGL:
Triangulate a polygon with additional control over the size of the triangles in the end result. For example, to triangulate a given polygon while making sure that every resulting triangle edge was no more than 10 centimeters long, you could use:
Polygon2d.triangulateWith
(Polygon2d.maxEdgeLength (Length.centimeters 10))
polygonNote that this means that existing polygon edges may be split into smaller ones, and new vertices will likely end up being added inside the polygon. Also note that the resulting triangles may still be very thin; no particular attempt is made to avoid large or small angles in triangles.
A TriangulationRule controls the polygon triangulation process and
determines how large the faces/edges are in the resulting triangular mesh.
Ensure that every edge in a triangulation has at most the given length.
Ensure that every triangle in a triangulation has at most the given width and the given height.