Orasund / elm-game-essentials / Location

This module contains two different type for repesenting a 2D Point: Location and Vector. The idea is to use Locations to store a Point and Vector to do caluclations. This way the compiler knows exactly what you are doing and can give you appropriate error messages.

Angle


type Angle
    = Angle Basics.Float

A Angle should store Elm angles (radians).

Use the functions degrees,radias,turns or pi to obtain a Angle.

Angle <| degree 90

toAngle : Vector -> Angle

An Angle can be constructed from a Vector.

{x=0,y=0} |> Location.toAngle
--> 0

Location


type alias Location =
( Basics.Float, Basics.Float )

A Location a point, possibly stored in a dictionary. Thats why its a tuple. But doing calulations with tuples can get very chaotic, thats why the module provides Vectors.

move : Basics.Float -> Angle -> Location -> Location

given a Location, a distance in some direction. This is the most used way how to change a location.

(0,0) |> move 1 (Angle 0)
--> (1,0)

add : Vector -> Location -> Location

simple adds a vector to a location, from geometry we know that a vector is actually just an angle and a length.

add ( fromAngle angle |> scaleby length)
--> move length angle

vectorTo : Location -> Location -> Vector

The difference between to locations is a vector.

loc1 <----- loc2

the resulting vector points from the second value to the first.

(1,0) |> vectorTo (1,1)
--> { x = 0, y = 1 }

Vector


type alias Vector =
{ x : Basics.Float, y : Basics.Float }

A Vector is used for calculations.

fromAngle : Angle -> Vector

construct a unit vector from an angle

fromAngle (Angle (pi/4) )
|> length
--> 1

distance : Location -> Location -> Basics.Float

The Distance between two locations is the length of the resulting vector

distance == vectorTo >> length

rotate : Angle -> Vector -> Vector

rotate a Vector

{ x = 1, y = 0 } |> rotate (Angle (pi/2))
--> {x = 0, y = -1}

scaleBy : Basics.Float -> Vector -> Vector

Scales a Vector. Vectors obtained by fromAngle have size 1.

fromAngle (Angle (pi/4) ) |> scaleBy 42 |> length
--> 42

length : Vector -> Basics.Float

Returns the length of the coordinate (distance to (0,0)