Created
February 9, 2017 04:44
-
-
Save tmcw/1a4b1dd9710ae9525bf5a4b5694b3005 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (..) | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onClick, onInput) | |
import Color exposing (..) | |
import Element exposing (..) | |
import Collage exposing (..) | |
import List exposing (map) | |
import List.Extra exposing ((!!)) | |
import Tuple exposing (first, second) | |
import Random | |
main = | |
Html.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
dotcount : Int | |
dotcount = | |
200 | |
size : Int | |
size = | |
18 * 150 | |
type Msg | |
= DotPositions (List ( Float, Float )) | |
| Tails (List (List Int)) | |
| SetCount String | |
| Place | |
| PlaceTails | |
type alias Model = | |
{ dotPositions : List ( Float, Float ) | |
, tails : List (List Int) | |
, count : Int | |
} | |
init : ( Model, Cmd Msg ) | |
init = | |
( Model [] [] 10, generateDots ) | |
generateDots : Cmd Msg | |
generateDots = | |
Random.generate DotPositions | |
(Random.list | |
dotcount | |
(Random.pair inRange inRange) | |
) | |
generateTails : Cmd Msg | |
generateTails = | |
Random.generate Tails | |
(Random.list | |
20 | |
(Random.list 3 (Random.int 0 100)) | |
) | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
Place -> | |
( model | |
, generateDots | |
) | |
SetCount count -> | |
case String.toInt count of | |
Ok val -> | |
( Model model.dotPositions model.tails val | |
, Cmd.none | |
) | |
Err msg -> | |
( Model model.dotPositions model.tails 10 | |
, Cmd.none | |
) | |
PlaceTails -> | |
( model | |
, generateTails | |
) | |
Tails tails -> | |
( Model model.dotPositions tails model.count, Cmd.none ) | |
DotPositions positions -> | |
( Model positions model.tails model.count, Cmd.none ) | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none | |
inRange : Random.Generator Float | |
inRange = | |
Random.float (toFloat -size / 2.05) (toFloat size / 2.05) | |
getCoords : List ( Float, Float ) -> List Int -> List ( Float, Float ) | |
getCoords dots indexes = | |
List.map (\i -> Maybe.withDefault ( 0, 0 ) ((!!) dots i)) indexes | |
euclidean : ( Float, Float ) -> ( Float, Float ) -> Float | |
euclidean a b = | |
sqrt | |
(((first a) | |
- (first b) | |
) | |
^ 2 | |
+ ((second a) | |
- (second b) | |
) | |
^ 2 | |
) | |
distanceTo : ( Float, Float ) -> ( Float, Float ) -> ( Float, Float ) -> Order | |
distanceTo reference a b = | |
case (euclidean a reference) > (euclidean b reference) of | |
True -> | |
GT | |
False -> | |
LT | |
splitPath : Int -> List ( Float, Float ) -> List ( Float, Float ) -> List ( Float, Float ) | |
splitPath count head tail = | |
case List.length head == count of | |
True -> | |
head | |
False -> | |
case (List.head (List.reverse head)) of | |
Just a -> | |
let | |
sorted = | |
(List.sortWith (distanceTo a) tail) | |
in | |
case ( List.head sorted, List.tail sorted ) of | |
( Just h, Just t ) -> | |
(splitPath count (head ++ [ h ]) t) | |
_ -> | |
[] | |
Nothing -> | |
[] | |
shortPathFromOne : Int -> List ( Float, Float ) -> List ( Float, Float ) | |
shortPathFromOne count dots = | |
case ( List.head dots, List.tail dots ) of | |
( Just a, Just b ) -> | |
splitPath count [ a ] b | |
_ -> | |
[] | |
view : Model -> Html Msg | |
view model = | |
let | |
triangles = | |
shortPathFromOne model.count model.dotPositions | |
in | |
div [] | |
[ Html.button [ onClick Place ] [ Html.text "dots" ] | |
, Html.button [ onClick PlaceTails ] [ Html.text "tails" ] | |
, Html.input [ Html.Attributes.min "2", Html.Attributes.max (toString dotcount), type_ "range", onInput SetCount ] [] | |
, toHtml | |
(collage size | |
size | |
((List.map | |
(\c -> | |
(circle 10 |> filled black |> (move ( first c, second c ))) | |
) | |
model.dotPositions | |
) | |
++ [ (path triangles |> traced { defaultLine | width = 10, color = black, dashing = [ 12, 12 ], join = Smooth }) ] | |
) | |
) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment