opensolid/geometry/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

OpenSolid.LineSegment2d

LineSegment2d

A LineSegment2d is a line between two points in 2D. This module contains functionality such as:

  • Interpolating within a line segment or measuring its length
  • Scaling, rotating, translating, mirroring or projecting a line segment
  • Converting a line segment between local and global coordinates in different reference frames

Line segments can be constructed by passing a tuple of start and end points to the LineSegment2d constructor, for example

exampleLineSegment =
    LineSegment2d
        ( Point2d ( 1, 2 )
        , Point2d ( 3, 4 )
        )

Endpoints

startPoint : LineSegment2d -> Point2d

Get the start point of a line segment.

LineSegment2d.startPoint exampleLineSegment
--> Point2d ( 1, 2 )
endPoint : LineSegment2d -> Point2d

Get the end point of a line segment.

LineSegment2d.endPoint exampleLineSegment
--> Point2d ( 3, 4 )
endpoints : LineSegment2d -> ( Point2d, Point2d )

Get the endpoints of a line segment as a tuple.

( p1, p2 ) =
    LineSegment2d.endpoints lineSegment
reverse : LineSegment2d -> LineSegment2d

Reverse a line segment, swapping its start and end points.

LineSegment2d.reverse exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( 3, 4 )
-->     , Point2d ( 1, 2 )
-->     )

Interpolation

midpoint : LineSegment2d -> Point2d

Get the midpoint of a line segment.

LineSegment2d.midpoint exampleLineSegment
--> Point2d ( 2, 3 )
interpolate : LineSegment2d -> Float -> Point2d

Interpolate a line segment between its start and end points; a value of 0.0 corresponds to the start point of the line segment, a value of 0.5 corresponds to its midpoint and a value of 1.0 corresponds to its end point. Negative values or values greater than 1 can be used to extrapolate.

LineSegment2d.interpolate exampleLineSegment 0.25
--> Point2d ( 1.5, 2.5 )


LineSegment2d.interpolate exampleLineSegment 1.5
--> Point2d ( 4, 5 )

Length and direction

length : LineSegment2d -> Float

Get the length of a line segment.

LineSegment2d.length exampleLineSegment
--> 2.8284
squaredLength : LineSegment2d -> Float

Get the squared length of a line segment. Slightly more efficient than length since it avoids a square root.

LineSegment2d.squaredLength exampleLineSegment
--> 8
direction : LineSegment2d -> Maybe Direction2d

Get the direction from a line segment's start point to its end point. If the line segment has zero length (the start and end points are the same), returns Nothing.

LineSegment2d.direction exampleLineSegment
--> Just (Direction2d ( 0.7071, 0.7071 ))
normalDirection : LineSegment2d -> Maybe Direction2d

Get the direction perpendicular to a line segment, pointing to the left. If the line segment has zero length, returns Nothing.

LineSegment2d.normalDirection exampleLineSegment
--> Just (Direction2d ( -0.7071, 0.7071 ))
vector : LineSegment2d -> Vector2d

Get the vector from a given line segment's start point to its end point.

LineSegment2d.vector exampleLineSegment
--> Vector2d ( 2, 2 )

Transformations

Transforming a line segment is equivalent to transforming its start and end points and forming a new line segment between the resulting points.

scaleAbout : Point2d -> Float -> LineSegment2d -> LineSegment2d

Scale a line segment about the given center point by the given scale.

point =
    Point2d ( 1, 1 )


LineSegment2d.scaleAbout point 2 exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( 1, 3 )
-->     , Point2d ( 5, 7 )
-->     )
rotateAround : Point2d -> Float -> LineSegment2d -> LineSegment2d

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

LineSegment2d.rotateAround Point2d.origin (degrees 90) exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( -2, 1 )
-->     , Point2d ( -4, 3 )
-->     )
translateBy : Vector2d -> LineSegment2d -> LineSegment2d

Translate a line segment by a given displacement.

displacement =
    Vector2d ( 1, 2 )


LineSegment2d.translateBy displacement exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( 2, 4 )
-->     , Point2d ( 4, 6 )
-->     )
mirrorAcross : Axis2d -> LineSegment2d -> LineSegment2d

Mirror a line segment across an axis.

LineSegment2d.mirrorAcross Axis2d.y exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( -1, 2 )
-->     , Point2d ( -3, 4 )
-->     )

Note that the endpoints of a mirrored segment are equal to the mirrored endpoints of the original segment, but as a result the normal direction of a mirrored segment is the opposite of the mirrored normal direction of the original segment (since the normal direction is always considered to be 'to the left' of the line segment).

projectOnto : Axis2d -> LineSegment2d -> LineSegment2d

Project a line segment onto an axis.

LineSegment2d.projectOnto Axis2d.x exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( 1, 0 )
-->     , Point2d ( 3, 0 )
-->     )


LineSegment2d.projectOnto Axis2d.y exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( 0, 2 )
-->     , Point2d ( 0, 4 )
-->     )
map : (Point2d -> Point2d) -> LineSegment2d -> LineSegment2d

Transform the start and end points of a line segment by a given function and create a new line segment from the resulting points. Most other transformation functions can be defined in terms of map; for example,

LineSegment2d.projectOnto Axis2d.x

is equivalent to

LineSegment2d.map (Point2d.projectOnto Axis2d.x)

Coordinate frames

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

relativeTo : Frame2d -> LineSegment2d -> LineSegment2d

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

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


LineSegment2d.relativeTo localFrame exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( 0, 0 )
-->     , Point2d ( 2, 2 )
-->     )
placeIn : Frame2d -> LineSegment2d -> LineSegment2d

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

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


LineSegment2d.placeIn localFrame exampleLineSegment
--> LineSegment2d
-->     ( Point2d ( 2, 4 )
-->     , Point2d ( 4, 6 )
-->     )

Sketch planes

placeOnto : SketchPlane3d -> LineSegment2d -> LineSegment3d

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

LineSegment2d.placeOnto SketchPlane3d.yz exampleLineSegment
--> LineSegment3d
-->     ( Point3d ( 0, 1, 2 )
-->     , Point3d ( 0, 3, 4 )
-->     )

Bounds

boundingBox : LineSegment2d -> BoundingBox2d

Get the minimal bounding box containing a given line segment.

LineSegment2d.boundingBox exampleLineSegment
--> BoundingBox2d
-->     { minX = 1
-->     , maxX = 3
-->     , minY = 2
-->     , maxY = 4
-->     }