Paste: Coordinates
        
	
	
	
		| Author:  | Søren Enevoldsen | 
		| Mode:  | factor | 
		| Date:  | Sun, 28 Mar 2010 20:42:51 | 
	
	Plain Text |
	
	
USING: io arrays kernel combinators continuations sequences splitting
math math.parser ascii math.vectors math.functions math.trig ;
IN: coords
: distance ( coord coord -- distance ) [ - ] 2map [ 2 ^ ] map first2 + sqrt ;
: move-coord ( coord angle afstand -- new-coord )
    [ deg>rad [ sin ] [ cos ] bi ] dip [ * ] curry bi@
    2array [ + ] 2map ;
    
: usage ( -- ) "Indtast:\nafstand koord1 - koord2\nflyt koord - vinkel afstand\nafslut - afslutter programmet\n\nKOORD: 053 072 f.eks.\n" print flush ;
: decimals ( n decimals -- n' ) [ 10 swap ^ * round ] keep neg 10 swap ^ * ;
: invalid-input ( -- ) "Ugyldigt input. Proev igen" print flush ;
: wrong-num-of-elements ( -- ) "Forkert antal elementer" print flush ;
: str>num/f ( str -- num/f ) dup string>number [ nip ] [ "Fejl ved: " prepend print flush f ] if* ;
: parse-distance ( seq -- coord coord ) [ str>num/f ] map first4 [ 2array ] 2dip 2array ;
: parse-move ( seq -- coord angle afstand ) [ str>num/f ] map first4 [ 2array ] 2dip ;
: write-distance ( afstand -- ) 3 decimals number>string " = " prepend print flush ;
: write-move ( coord -- ) [ 3 decimals number>string ] map first2 [ [ " = " ] dip append " " append ] dip append print flush ;
: new-entry ( -- ) "\n----------------------------\n" print flush ;
DEFER: repl
: task ( command -- ) " " split [ "-" ] dip remove
    [ rest ] [ first >lower ] bi
  [
      { { "afstand" [ dup length 4 = [ parse-distance distance write-distance ]
                [ drop wrong-num-of-elements ] if  repl ] }
        { "flyt" [ dup length 4 = [ parse-move move-coord write-move ]
                [ drop wrong-num-of-elements ] if repl ] }
        { "afslut" [ nl drop ] }
        [ 2drop invalid-input repl ] } case
  ]
  [ 3drop "Noget var forkert. Proev igen." print flush ] recover repl ;
: repl ( -- ) new-entry readln task ;
: startup ( -- ) usage repl ;
MAIN: startup
	
	
		New Annotation