ianmackenzie/elm-geometry/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Vector2d

Vector2d

A Vector2d represents a quantity such as a displacement or velocity in 2D, and is defined by its X and Y components. This module contains a variety of vector-related functionality, such as

  • Adding or subtracting vectors
  • Finding the lengths of vectors
  • Rotating vectors
  • Converting vectors between different coordinate systems

Note that unlike in many other geometry packages where vectors are used as a general-purpose data type, elm-geometry has separate data types for vectors, directions and points. In most code it is actually more common to use Point2d and Direction2d than Vector2d, and much code can avoid working directly with Vector2d values at all!

type alias Vector2d =
Vector2d

Constants

zero : Vector2d

Although there are no predefined constants for the vectors with components (1, 0) and (0, 1), in most cases you will actually want their Direction2d versions Direction2d.x and Direction2d.y.

Constructors

fromComponents : ( Float, Float ) -> Vector2d
fromPolarComponents : ( Float, Float ) -> Vector2d
from : Point2d -> Point2d -> Vector2d
withLength : Float -> Direction2d -> Vector2d
perpendicularTo : Vector2d -> Vector2d
interpolateFrom : Vector2d -> Vector2d -> Float -> Vector2d

Properties

components : Vector2d -> ( Float, Float )
xComponent : Vector2d -> Float
yComponent : Vector2d -> Float
polarComponents : Vector2d -> ( Float, Float )
length : Vector2d -> Float
squaredLength : Vector2d -> Float
direction : Vector2d -> Maybe Direction2d
lengthAndDirection : Vector2d -> Maybe ( Float, Direction2d )

Comparison

equalWithin : Float -> Vector2d -> Vector2d -> Bool

Measurement

componentIn : Direction2d -> Vector2d -> Float

Arithmetic

sum : Vector2d -> Vector2d -> Vector2d
difference : Vector2d -> Vector2d -> Vector2d
dotProduct : Vector2d -> Vector2d -> Float
crossProduct : Vector2d -> Vector2d -> Float

Transformations

Note that for mirrorAcross and projectOnto, only the direction of the axis affects the result, since vectors are position-independent. Think of mirroring/projecting a vector across/onto an axis as moving the vector so its tail is on the axis, then mirroring/projecting its tip across/onto the axis.

reverse : Vector2d -> Vector2d
normalize : Vector2d -> Vector2d
scaleBy : Float -> Vector2d -> Vector2d
rotateBy : Float -> Vector2d -> Vector2d
rotateClockwise : Vector2d -> Vector2d
rotateCounterclockwise : Vector2d -> Vector2d
mirrorAcross : Axis2d -> Vector2d -> Vector2d
projectionIn : Direction2d -> Vector2d -> Vector2d
projectOnto : Axis2d -> Vector2d -> Vector2d

Coordinate conversions

Like other transformations, coordinate conversions of vectors depend only on the orientations of the relevant frames, not the positions of their origin points.

relativeTo : Frame2d -> Vector2d -> Vector2d
placeIn : Frame2d -> Vector2d -> Vector2d