lue-bird/elm-xy/1.1.0

NOTE: the latest version is 1.1.3

Xy

type alias Xy coordinate =
( coordinate, coordinate )

A tuple, containing 2 coordinates of the same type.

create

xy : coordinate -> coordinate -> Xy coordinate

Construct a Xy from first the x, the the y coordinate.

both : coordinate -> Xy coordinate

Construct a Xy from the same value for the x & y coordinates.

Xy.both 0 --> ( 0, 0 )
zero : Xy numberCoordinate

Construct a Xy where the x & y coordinates are 0.

Xy.zero --> ( 0, 0 )
one : Xy numberCoordinate

Construct a Xy where the x & y coordinates are 1.

Xy.one --> ( 1, 1 )
direction : Float -> Xy Float

Express the angle as a unit vector.

Xy.direction (turns (1/6))
--> ( 0.5000000000000001, 0.8660254037844386 )


fromLengthAndRotation length rotation =
    Xy.direction rotation
        >> Xy.map ((*) length)

from record

fromXY : { record | x : coordinate, y : coordinate } -> Xy coordinate

Construct a Xy from the x and y fields contained in a record.

initialPosition : Xy Float
initialPosition =
    Browser.getViewport
        |> Task.perform (.viewport >> Xy.fromXY)

Browser.getViewport link.

{ record | width : coordinate, height : coordinate }
-> Xy coordinate

Construct a Xy from the width and height fields contained in a record.

initialSize : Xy Float
initialSize =
    Browser.getViewport
        |> Task.perform (.viewport >> Xy.fromSize)

Browser.getViewport link.

scan

x : Xy coordinate -> coordinate

The x coordinate.

( 3, 5 ) |> Xy.x --> 3
y : Xy coordinate -> coordinate

The y coordinate.

( 3, 5 ) |> Xy.y --> 5
length : Xy Float -> Float

The length of the hypotenuse

 ^
y|\
 | \ length
 |  \
 0--->
    x

modify

mapX : (coordinate -> coordinate) -> Xy coordinate -> Xy coordinate

Update the x coordinate based on its current value.

flipX =
    XY.mapX (\x -> -x)
mapY : (coordinate -> coordinate) -> Xy coordinate -> Xy coordinate

Update the y coordinate based on its current value.

flipY =
    XY.mapY (\y -> -y)

transform

map :
(coordinate -> mappedCoordinate)
-> Xy coordinate
-> Xy mappedCoordinate

Apply the same function to both coordinates.

( 4.567, 4.321 ) |> Xy.map round
--> ( 5, 4 ) : Xy Int


opposite =
    Xy.map (\coord -> -coord)


scale factor =
    Xy.map ((*) factor)
(coordinate -> mappedCoordinate)
-> (coordinate -> mappedCoordinate)
-> Xy coordinate
-> Xy mappedCoordinate

Apply a different function to the x & y coordinate.

( 4.567, 4.321 ) |> Xy.mapXY floor ceiling
--> ( 4, 5 ) : Xy Int
(aCoordinate -> bCoordinate -> combinedCoordinate)
-> Xy aCoordinate
-> Xy bCoordinate
-> Xy combinedCoordinate

Apply a function combining the x & y coordinates of 2 Xys.

Xy.map2 (\a b -> a - b) ( 10, 5 ) ( 5, 1 )
--> ( 5, 4 )


Xy.map2 (+) ( 10, 5 ) ( 5, 1 )
--> ( 15, 6 )
toAngle : Xy Float -> Float

The angle (in radians).

     ^
    /|y
   / |
  /⍺ |
 0--->
    x
Xy.toAngle ( 1, 1 ) --> pi/4 radians or 45°


Xy.toAngle ( -1, 1 ) --> 3*pi/4 radians or 135°


Xy.toAngle ( -1, -1 ) --> -3*pi/4 radians or 225°


Xy.toAngle ( 1, -1 ) --> -pi/4 radians or 315°
to : (coordinate -> coordinate -> result) -> Xy coordinate -> result

Use the x and the y coordinate to return a result.

( 3, 4 ) |> Xy.to max --> 4
normalize : Xy Float -> Xy Float

Convert to a unit vector.

( 3, 4 ) |> Xy.normalize
--> ( 3/5, 4/5 )

extra

serialize : Codec error coordinate -> Codec error (Xy coordinate)

A Codec to serialize Xys.

serializePoint : Serialize.Codec error (Xy Int)
serializePoint =
    Xy.serialize Serialize.int
random : Xy (Generator coordinate) -> Generator (Xy coordinate)

A Generator for random Xys.

randomSpeed =
    Xy.random (XY.both (Random.float -1 1))


randomPoint =
    Xy.random
        ( Random.float left right
        , Random.float bottom top
        )