ianmackenzie/elm-geometry/2.0.0

NOTE: the latest version is 4.0.0

Triangle2d

A Triangle2d represents a triangle in 2D space, and is defined by its three vertices. This module contains triangle-related functionality such as:

  • Finding the area and centroid of triangles
  • Scaling, rotating, translating and mirroring triangles
  • Converting triangles between different coordinate systems
type alias Triangle2d units coordinates =
Triangle2d units coordinates

Constructors

( Point2d units coordinates
, Point2d units coordinates
, Point2d units coordinates
)
-> Triangle2d units coordinates

Construct a triangle from its three vertices:

exampleTriangle =
    Triangle2d.fromVertices
        ( Point2d.meters 1 1
        , Point2d.meters 2 1
        , Point2d.meters 1 3
        )
Point2d units coordinates
-> Point2d units coordinates
-> Point2d units coordinates
-> Triangle2d units coordinates

Construct a triangle from the first point, to the second, to the third:

exampleTriangle =
    Triangle2d.from
        (Point2d.meters 1 1)
        (Point2d.meters 2 1)
        (Point2d.meters 1 3)

Useful with map3 functions such as Json.Decode.map3.

Properties

Triangle2d units coordinates
-> ( Point2d units coordinates
, Point2d units coordinates
, Point2d units coordinates
)

Get the vertices of a triangle.

( p1, p2, p3 ) =
    Triangle2d.vertices exampleTriangle


--> p1 = Point2d.meters 1 1
--> p2 = Point2d.meters 2 1
--> p3 = Point2d.meters 1 3
Triangle2d units coordinates
-> ( LineSegment2d units coordinates
, LineSegment2d units coordinates
, LineSegment2d units coordinates
)

Get the edges of a triangle: from the first vertex to the second, from the second to the third, and from the third back to the first.

( e1, e2, e3 ) =
    Triangle2d.edges exampleTriangle


--> e1 =
-->     LineSegment2d.from
-->         (Point2d.meters 1 1)
-->         (Point2d.meters 2 1)
-->
--> e2 =
-->     LineSegment2d.from
-->         (Point2d.meters 2 1)
-->         (Point2d.meters 1 3)
-->
--> e3 =
-->     LineSegment2d.from
-->         (Point2d.meters 1 3)
-->         (Point2d.meters 1 1)
centroid : Triangle2d units coordinates -> Point2d units coordinates

Get the centroid (center of mass) of a triangle.

Triangle2d.centroid exampleTriangle
--> Point2d.meters 1.3333 1.6667
area : Triangle2d units coordinates -> Quantity Float (Squared units)

Get the area of a triangle. The result will always be positive regardless of whether the triangle's vertices are in clockwise or counterclockwise order.

Triangle2d.area exampleTriangle
--> Area.squareMeters 1.0
counterclockwiseArea : Triangle2d units coordinates -> Quantity Float (Squared units)

Get the signed area of a triangle, returning a positive value if the triangle's vertices are in counterclockwise order and a negative value otherwise.

Triangle2d.counterclockwiseArea exampleTriangle
--> Area.squareMeters 1.0
clockwiseArea : Triangle2d units coordinates -> Quantity Float (Squared units)

Get the signed area of a triangle, returning a positive value if the triangle's vertices are in clockwise order and a negative value otherwise.

Triangle2d.clockwiseArea exampleTriangle
--> Area.squareMeters -1.0
boundingBox : Triangle2d units coordinates -> BoundingBox2d units coordinates

Get the minimal bounding box containing a given triangle.

Triangle2d.boundingBox exampleTriangle
--> BoundingBox2d.fromExtrema
-->     { minX = Length.meters 1
-->     , maxX = Length.meters 2
-->     , minY = Length.meters 1
-->     , maxY = Length.meters 3
-->     }
Triangle2d units coordinates
-> Maybe (Circle2d units coordinates)

Attempt to find the circumcircle of a triangle, a circle that passes through each of the triangle's vertices;

Triangle2d.circumcircle triangle

is equivalent to

let
    ( p1, p2, p3 ) =
        Triangle2d.vertices triangle
in
Circle2d.throughPoints p1 p2 p3

If the triangle is degenerate (its three vertices are collinear), returns Nothing.

Queries

contains : Point2d units coordinates -> Triangle2d units coordinates -> Bool

Check whether a given point is inside a given triangle.

interiorPoint =
    Point2d.meters 1.5 1.5


Triangle2d.contains interiorPoint exampleTriangle
--> True


Triangle2d.contains Point2d.origin exampleTriangle
--> False

It does not matter whether the triangle's vertices are in clockwise or counterclockwise order.

Unit conversions

at :
Quantity Float (Rate units2 units1)
-> Triangle2d units1 coordinates
-> Triangle2d units2 coordinates

Convert a triangle 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.

at_ :
Quantity Float (Rate units1 units2)
-> Triangle2d units1 coordinates
-> Triangle2d units2 coordinates

Convert a triangle 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.

Transformations

These transformations generally behave just like the ones in the Point2d module.

Point2d units coordinates
-> Float
-> Triangle2d units coordinates
-> Triangle2d units coordinates

Scale a triangle about a given point by a given scale. Note that scaling by a negative value will result in the 'winding direction' of the triangle being flipped - if the triangle's vertices were in counterclockwise order before the negative scaling, they will be in clockwise order afterwards and vice versa.

Point2d units coordinates
-> Angle
-> Triangle2d units coordinates
-> Triangle2d units coordinates

Rotate a triangle around a given point by a given angle.

Vector2d units coordinates
-> Triangle2d units coordinates
-> Triangle2d units coordinates

Translate a triangle by a given displacement.

Direction2d coordinates
-> Quantity Float units
-> Triangle2d units coordinates
-> Triangle2d units coordinates

Translate a triangle in a given direction by a given distance.

Axis2d units coordinates
-> Triangle2d units coordinates
-> Triangle2d units coordinates

Mirror a triangle across a given axis. Note that mirroring a triangle will result in its 'winding direction' being flipped - if the triangle's vertices were in counterclockwise order before mirroring, they will be in clockwise order afterwards and vice versa.

(Point2d unitsA coordinatesA -> Point2d unitsB coordinatesB)
-> Triangle2d unitsA coordinatesA
-> Triangle2d unitsB coordinatesB

Transform each vertex of a triangle by a given function and create a new triangle from the resulting points. Most other transformation functions can be defined in terms of mapVertices; for example,

Triangle2d.mirrorAcross axis

is equivalent to

Triangle2d.mapVertices (Point2d.mirrorAcross axis)

Coordinate conversions

Frame2d units globalCoordinates { defines : localCoordinates }
-> Triangle2d units globalCoordinates
-> Triangle2d units localCoordinates

Take a triangle defined in global coordinates, and return it expressed in local coordinates relative to a given reference frame.

Frame2d units globalCoordinates { defines : localCoordinates }
-> Triangle2d units localCoordinates
-> Triangle2d units globalCoordinates

Take a triangle considered to be defined in local coordinates relative to a given reference frame, and return that triangle expressed in global coordinates.