opensolid/geometry/1.0.3

NOTE: this package is not compatible with Elm 0.19.1

OpenSolid.Triangle2d

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

Triangles can be constructed by passing a tuple of vertices to the Triangle2d constructor, for example

exampleTriangle =
    Triangle2d
        ( Point2d ( 1, 1 )
        , Point2d ( 2, 1 )
        , Point2d ( 1, 3 )
        )

Accessors

vertices : Triangle2d -> ( Point2d, Point2d, Point2d )

Get the vertices of a triangle.

( p1, p2, p3 ) =
    Triangle2d.vertices exampleTriangle
--> p1 = Point2d ( 1, 1 )
--> p2 = Point2d ( 2, 1 )
--> p3 = Point2d ( 1, 3 )
edges : Triangle2d -> ( LineSegment2d, LineSegment2d, LineSegment2d )

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 ( Point2d ( 1, 1 ), Point2d ( 2, 1 ) )
--> e2 = LineSegment2d ( Point2d ( 2, 1 ), Point2d ( 1, 3 ) )
--> e3 = LineSegment2d ( Point2d ( 1, 3 ), Point2d ( 1, 1 ) )

Basics

centroid : Triangle2d -> Point2d

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

Triangle2d.centroid exampleTriangle
--> Point2d ( 1.3333, 1.6667 )
contains : Point2d -> Triangle2d -> Bool

Check whether a given point is inside a given triangle.

interiorPoint =
    Point2d ( 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.

Area

area : Triangle2d -> Float

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
--> 1.0
counterclockwiseArea : Triangle2d -> Float

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
--> 1.0
clockwiseArea : Triangle2d -> Float

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
--> -1.0

Transformations

Transforming a triangle is equivalent to transforming its vertices.

scaleAbout : Point2d -> Float -> Triangle2d -> Triangle2d

Scale a triangle about a given point by a given scale.

Triangle2d.scaleAbout Point2d.origin 2 exampleTriangle
--> Triangle2d
-->     ( Point2d ( 2, 2 )
-->     , Point2d ( 4, 2 )
-->     , Point2d ( 2, 6 )
-->     )

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.

rotateAround : Point2d -> Float -> Triangle2d -> Triangle2d

Rotate a triangle around a given point by a given angle (in radians).

Triangle2d.rotateAround Point2d.origin (degrees 90) exampleTriangle
--> Triangle2d
-->     ( Point2d ( -1, 1 )
-->     , Point2d ( -1, 2 )
-->     , Point2d ( -3, 1 )
-->     )
translateBy : Vector2d -> Triangle2d -> Triangle2d

Translate a triangle by a given displacement.

displacement =
    Vector2d ( 2, -3 )


Triangle2d.translateBy displacement exampleTriangle
--> Triangle2d
-->     ( Point2d ( 3, -2 )
-->     , Point2d ( 4, -2 )
-->     , Point2d ( 3, 0 )
-->     )
mirrorAcross : Axis2d -> Triangle2d -> Triangle2d

Mirror a triangle across a given axis.

Triangle2d.mirrorAcross Axis2d.y exampleTriangle
--> Triangle2d
-->     ( Point2d ( -1, 1 )
-->     , Point2d ( -2, 1 )
-->     , Point2d ( -1, 3 )
-->     )

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.

map : (Point2d -> Point2d) -> Triangle2d -> Triangle2d

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 map; for example,

Triangle.mirrorAcross Axis2d.x triangle

is equivalent to

Triangle.map (Point2d.mirrorAcross Axis2d.x) triangle

Coordinate frames

Functions for transforming triangles between local and global coordinates in different coordinate frames.

relativeTo : Frame2d -> Triangle2d -> Triangle2d

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

localFrame =
    Frame2d.at (Point2d ( 1, 2 ))


Triangle2d.relativeTo localFrame exampleTriangle
--> Triangle2d
-->     ( Point2d ( 0, -1 )
-->     , Point2d ( 1, -1 )
-->     , Point2d ( 0, 1 )
-->     )
placeIn : Frame2d -> Triangle2d -> Triangle2d

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

localFrame =
    Frame2d.at (Point2d ( 1, 2 ))


Triangle2d.placeIn localFrame exampleTriangle
--> Triangle2d
-->     ( Point2d ( 2, 3 )
-->     , Point2d ( 3, 3 )
-->     , Point2d ( 2, 5 )
-->     )

Sketch planes

placeOnto : SketchPlane3d -> Triangle2d -> Triangle3d

Take a triangle defined in 2D coordinates within a particular sketch plane and return the corresponding triangle in 3D.

Triangle2d.placeOnto SketchPlane3d.xz exampleTriangle
--> Triangle3d
-->     ( Point3d ( 1, 0, 1 )
-->     , Point3d ( 2, 0, 1 )
-->     , Point3d ( 1, 0, 3 )
-->     )

Bounds

boundingBox : Triangle2d -> BoundingBox2d

Get the minimal bounding box containing a given triangle.

Triangle2d.boundingBox exampleTriangle
--> BoundingBox2d
-->     { minX = 1
-->     , maxX = 2
-->     , minY = 1
-->     , maxY = 3
-->     }