Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A relativity calculator for finding the Lorentz factor at various velocities
module Relativity exposing (main)
import Html exposing (..)
import Html.Attributes exposing (style, src, min, max, type_, value)
import Html.Events exposing (onClick, onInput, onMouseEnter, onMouseDown, onMouseUp)
type Msg
= SetMacro String
| SetMedium String
| SetMicro String
type alias Model =
{ macro : Float
, medium : Float
, micro : Float
}
init : ( Model, Cmd Msg )
init =
( Model 50 0 35
, Cmd.none
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SetMacro x ->
{ model | macro = Result.withDefault 0 (String.toFloat x) } ! []
SetMedium x ->
{ model | medium = Result.withDefault 0 (String.toFloat x) } ! []
SetMicro x ->
{ model | micro = Result.withDefault 0 (String.toFloat x) } ! []
view : Model -> Html Msg
view model =
let
v =
totalSpeed model
c =
299792458
in
div [ Html.Attributes.style [ ( "font-family", "'Bitter', 'Nunito', 'Helvetica Neue', 'Helvetica', sans-serif" ), ( "font-size", "18px" ), ( "color", "#333333" ), ( "background", "whitesmoke" ), ( "padding", "16px" ) ] ]
[ p []
[ text <| "Macro velocity: " ++ toString model.macro ++ "% of c" ]
, Html.node
"style"
[ type_ "text/css" ]
[ Html.text "@import url('https://fonts.googleapis.com/css?family=Bitter')" ]
, input
[ type_ "range"
, Html.Attributes.min "0"
, Html.Attributes.max "99"
, Html.Attributes.step "1"
, Html.Attributes.style [ ( "width", "95%" ) ]
, onInput SetMacro
]
[]
, p [] [ text <| "Medium velocity: " ++ toString model.medium ++ "m/s" ]
, input
[ type_ "range"
, Html.Attributes.min "0"
, Html.Attributes.max (toString (c / 100 - 1000))
, Html.Attributes.style [ ( "width", "95%" ) ]
, onInput SetMedium
]
[]
, p [] [ text <| "Micro velocity: " ++ toString model.micro ++ "m/s" ]
, input
[ type_ "range"
, Html.Attributes.min "0"
, Html.Attributes.max "1000"
, Html.Attributes.step "1"
, Html.Attributes.style [ ( "width", "95%" ) ]
, onInput SetMicro
]
[]
, p [] [ text <| "Total velocity: " ++ toString v ++ "m/s" ]
, p [] [ text <| "Total velocity as percentage: " ++ toString (100 * v / c) ++ "%c" ]
, p [] [ text <| "Lorentz factor: " ++ toString (1 / ((sqrt (1 - ((v * v) / (c * c)))))) ]
, p [] [ text <| "Inverse Lorentz factor: " ++ toString (((sqrt (1 - ((v * v) / (c * c)))))) ]
]
totalSpeed model =
2997924.58 * model.macro + model.medium + model.micro
main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.