Advertisement
IcarusLives

Rotating 2D Star

May 19th, 2018
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.59 KB | None | 0 0
  1. @echo off & setlocal enableDelayedExpansion & mode 60,60
  2.  
  3. rem these macros are LINE, PLOT, SIN, COS, ABS
  4. call :macros
  5.  
  6. rem initialize position, size, sides, angle, and rotation.
  7. set /a "x=30", "y=30", "radius=15", "sides=5", "angle=360 / sides", "rotationSpeed=12"
  8.  
  9. rem infinite loop
  10.     for /l %%# in () do (
  11.    
  12.         set /a "frame+=1", "changeColor=frame %% 3"
  13.         if !changeColor! equ 0 set /a "h+=1", "hue=h %% 256 + 16"
  14.        
  15.         rem calculate the rotation of the star
  16.         set /a "currentAngle+=rotationSpeed", "T_maxAngle=360 - angle + currentAngle", "p=0"
  17.         for /l %%c in (!currentAngle!,!angle!,!T_maxAngle!) do (
  18.             rem calculate the points of a polygon and grab every other point
  19.             set /a "p+=1", "n=p + 1", "tA=%%c + (angle * 2)"
  20.             set /a "x!p!=radius * !cos(x):x=%%c! + x", "y!p!=radius * !sin(x):x=%%c! + y"
  21.             set /a "i!n!=radius * !cos(x):x=tA!  + x", "j!n!=radius * !sin(x):x=tA!  + y"
  22.             rem draw the star
  23.             for /f "tokens=1,2" %%a in ("!p! !n!") do ( %line% !x%%a! !y%%a! !i%%b! !j%%b! !hue! )
  24.         )
  25.         rem display everything
  26.         <nul set /p "=%esc%[2J!screen!" & set "screen="
  27.     )
  28. rem ----------------------------------------------------------------------------
  29.  
  30.  
  31.  
  32.  
  33. :macros
  34. set ^"LF=^
  35.  
  36. ^" Above empty line is required - do not remove
  37. set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  38.  
  39. for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
  40. <nul set /p "=!esc![?25l"
  41.  
  42. rem %plot% x y 0-255 CHAR
  43. set plot=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-4" %%1 in ("^!args^!") do (%\n%
  44.   set "screen=^!screen^!!esc![%%2;%%1H!esc![38;5;%%3m%%~4!esc![0m"%\n%
  45. )) else set args=
  46.  
  47. rem line x1 y1 x2 y2 color
  48. set line=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-5" %%1 in ("^!args^!") do (%\n%
  49.     if "%%~5" equ "" ( set "hue=30" ) else ( set "hue=%%~5")%\n%
  50.     set /a "xa=%%~1", "ya=%%~2", "xb=%%~3", "yb=%%~4", "dx=%%~3 - %%~1", "dy=%%~4 - %%~2"%\n%
  51.     for /f "tokens=1-2" %%6 in ("^!dx^! ^!dy^!") do (%\n%
  52.         if %%~7 lss 0 ( set /a "dy=-%%~7", "stepy=-1" ) else ( set "stepy=1" )%\n%
  53.         if %%~6 lss 0 ( set /a "dx=-%%~6", "stepx=-1" ) else ( set "stepx=1" )%\n%
  54.         set /a "dx<<=1", "dy<<=1"%\n%
  55.     )%\n%
  56.     for /f "tokens=1-9" %%a in ("^!dx^! ^!dy^! ^!xa^! ^!xb^! ^!ya^! ^!yb^! ^!stepx^! ^!stepy^! ^!hue^!") do (%\n%
  57.         if %%~a gtr %%~b (%\n%
  58.             set /a "fraction=%%~b - (%%~a >> 1)"%\n%
  59.             for /l %%x in (%%~c,%%~g,%%~d) do (%\n%
  60.                 for /f "tokens=1" %%6 in ("^!fraction^!") do if %%~6 geq 0 set /a "ya+=%%~h", "fraction-=%%~a"%\n%
  61.                 set /a "fraction+=%%~b"%\n%
  62.                 for /f "tokens=1" %%6 in ("^!ya^!") do (%\n%
  63.                     if 0 leq %%x if %%x lss 199 if 0 leq %%~6 if %%~6 lss 199 ^!plot^! %%x %%~6 %%i Û%\n%
  64.                 )%\n%
  65.             )%\n%
  66.         ) else (%\n%
  67.             set /a "fraction=%%~a - (%%~b >> 1)"%\n%
  68.             for /l %%y in (%%~e,%%~h,%%~f) do (%\n%
  69.                 for /f "tokens=1" %%6 in ("^!fraction^!") do if %%~6 geq 0 set /a "xa+=%%~g", "fraction-=%%~b"%\n%
  70.                 set /a "fraction+=%%~a"%\n%
  71.                 for /f "tokens=1" %%6 in ("^!xa^!") do (%\n%
  72.                     if 0 leq %%~6 if %%~6 lss 199 if 0 leq %%y if %%y lss 199 ^!plot^! %%~6 %%y %%i Û%\n%
  73.                 )%\n%
  74.             )%\n%
  75.         )%\n%
  76.     )%\n%
  77. )) else set args=
  78.  
  79.     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"
  80.     set "SIN(x)=(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"
  81.     set "COS(x)=(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"
  82.     set "_SIN="
  83.     set "Abs(x)=(((x)>>31|1)*(x))"
  84.  
  85. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement