Skip to content

Instantly share code, notes, and snippets.

@folkertdev
Created January 27, 2017 10:54
Show Gist options
  • Save folkertdev/ec59e917a038c3f0d67bdac1595791f9 to your computer and use it in GitHub Desktop.
Save folkertdev/ec59e917a038c3f0d67bdac1595791f9 to your computer and use it in GitHub Desktop.
An elm function to calculate some appending function over the diagonals of a matrix (represented as a List (List a)))
module Main exposing (..)
import Html exposing (text)
import Math.Vector2 as Vec2 exposing (Vec2, vec2)
type alias Point =
{ location : Vec2, velocity : Vec2 }
distributeForce : List { a | location : Vec2, velocity : Vec2 } -> List { a | location : Vec2, velocity : Vec2 }
distributeForce points =
List.map .location points
|> fused distribute Vec2.add
|> List.map2 (\p added -> { p | velocity = Vec2.add p.velocity added }) points
fused : (a -> List a -> List a) -> (a -> a -> a) -> List a -> List a
fused expand append list =
case list of
x :: xs ->
case expand x xs of
[] ->
[]
z :: zs ->
z :: List.map2 append zs (fused expand append xs)
[] ->
[]
distribute : Vec2 -> List Vec2 -> List Vec2
distribute v vs =
let
helper v1 v2 =
let
d =
Vec2.sub v1 v2
distance =
Vec2.length d
direction =
Vec2.normalize d
in
( (Vec2.scale 0.1 direction)
, (Vec2.negate <| Vec2.scale 0.1 direction)
)
in
List.unzip (List.map (helper v) vs)
|> Tuple.mapFirst (List.foldr Vec2.add (vec2 0 0))
|> uncurry (::)
vectors =
[ vec2 1 1
, vec2 -1 1
, vec2 1 -1
, vec2 -1 -1
]
main =
vectors
|> List.map (\v -> { location = v, velocity = vec2 0 0 })
|> distributeForce
|> toString
|> text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment