Advertisement
IcarusLives

New TurtleGraphics

Feb 27th, 2019
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.33 KB | None | 0 0
  1. @echo off & setlocal enableDelayedExpansion
  2.  
  3. call :turtleGraphics
  4.  
  5. set "axiom=FX"
  6. set "var[1]=X"
  7. set "var[2]=Y"
  8. set "var[3]="
  9. set "rule[1]=X+YF+"
  10. set "rule[2]=-FX-Y"
  11. set "rule[3]="
  12. set "angle=90"
  13. set "generations=8"
  14.  
  15. call :build_LSystem
  16.  
  17. call :display_LSystem
  18.  
  19. pause > nul & exit
  20. rem The script is controlled entirely from above. All code below are FUNCTIONS to support the LSystem
  21. rem -------------------------------
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. :build_LSystem
  29.     for /l %%a in (0,1,%generations%) do ( set "sentenceLength=0" & set "s=!axiom!#" & set "len=0"
  30.         for %%P in (8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( if "!s:~%%P,1!" NEQ "" ( set /a "len+=%%P" & set "s=!s:~%%P!" ) )
  31.         set "sentenceLength=!len!" & set "nextSentence="
  32.         for /l %%i in (0,1,!sentenceLength!) do (
  33.             set "current=!axiom:~%%i,1!"
  34.                    if "!current!" equ "%var[1]%" ( set "nextSentence=!nextSentence!%rule[1]%"
  35.             ) else if "!current!" equ "%var[2]%" ( set "nextSentence=!nextSentence!%rule[2]%"
  36.             ) else if "!current!" equ "%var[3]%" ( set "nextSentence=!nextSentence!%rule[3]%"
  37.             ) else ( set "nextSentence=!nextSentence!!current!"
  38.             )
  39.         )
  40.         set "axiom=!nextSentence!" & <nul set /p "=%esc%[2JGenerations %%a/%generations%"
  41.     )
  42.     set "axiom=!axiom:X=!" & set "axiom=!axiom:Y=!" & echo. & set "s=!axiom!#" & set "len=0"
  43.     for %%P in (8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( if "!s:~%%P,1!" NEQ "" ( set /a "len+=%%P" & set "s=!s:~%%P!" ) )
  44.     set "sentenceLength=%len%" & echo Sentence Length %sentenceLength%
  45. goto :eof
  46.  
  47. :display_LSystem
  48. for /l %%i in (0,1,!sentenceLength!) do ( set "current=!axiom:~%%i,1!"
  49.            if "!current!" equ "F" ( %penDown%
  50.     ) else if "!current!" equ "G" ( %penDown%
  51.     ) else if "!current!" equ "-" ( set /a "%turnLeft:?=!angle!%"
  52.     ) else if "!current!" equ "+" ( set /a "%turnRight:?=!angle!%"
  53.     ) else if "!current!" equ "[" ( set /a "%push%"
  54.     ) else if "!current!" equ "]" ( set /a "%pop%"
  55.     )
  56.     <nul set /p "=!turtleGraphics!" & set "turtleGraphics="
  57. )
  58. goto :eof
  59.  
  60. :turtleGraphics
  61.     rem ////////////////////////////////////////////////////////
  62.     rem The dependancies for the new turtleGraphics, like the old, but shorter names
  63.     set /a "HEI=WID=100"
  64.  
  65.     set /a "DFX=75","DFY=15","DFA=90"
  66.     mode %wid%,%hei%
  67.  
  68.     rem OTHER dependancies
  69.     set "_SIN=a-a*a/1920*a/312500+a*a/1920*a/15625*a/15625*a/2560000-a*a/1875*a/15360*a/15625*a/15625*a/16000*a/44800000"
  70.     set "SIN=(a=(x * 31416 / 180)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 10000"
  71.     set "COS=(a=(15708 - x * 31416 / 180)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 10000"
  72.     for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
  73.  
  74.     rem ALL turtleGraphics functions rewritten as quick macros
  75.     set "forward=DFX+=(?+1)*^!cos:x=DFA^!, DFY+=(?+1)*^!sin:x=DFA^!"
  76.     set "turnLeft=DFA-=(?)"
  77.     set "turnRight=DFA+=(?)"
  78.     set "push=sX=DFX, sY=DFY, sA=DFA"
  79.     set "pop=DFX=sX, DFY=sY, DFA=sA"
  80.     set "draw=?=^!?^!%esc%[^!DFY^!;^!DFX^!HÛ"
  81.     set "home=DFX=0, DFY=0, DFA=0"
  82.     set "cent=DFX=wid/2, DFY=hei/2"
  83.     set "penDown=for %%a in (1 2) do set /a "^!forward:?=1^!" ^& set "^!draw:?=turtleGraphics^!""
  84.     rem \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  85. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement