Pgen 0.1g [DemoBatch]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Pgen 0.1g [DemoBatch]

#1 Post by einstein1969 » 11 Apr 2014 13:28

Hi to all,

I perfected the fast sin(x) and this is a demo.

This is a code snippet. There is no color. But it is possible use cscript and other utility for colorize and speed

I used a gradient, I hope it is appreciated

EDIT: The Previus version is at the bottom

PGEN version 0.1g:

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PGEN by Francesco Poscetti aka einstein1969 - 2014

:: For best view use Lucida Console 5 o less.

:: WORKING ON v. 0.2  Add Trasparency/Function Mix at highRes.(DONE!). Keyboard usage for Zoom and Pan.

:: v. 0.1g Correct a bug on zoom at highres. Add comment for obscure/not well known trick.
::         Tune speed: gain another 12% off in High resolution.

:: v. 0.1f Add parameter for run in High resolution Lucida console 2.
::         Tune speed. About 15% more speed in High resolution (tested @800x300)

:: v. 0.1e Added variable PI, PI2, 2PI for code readability and other comments.

:: v. 0.1d Patched gradient for better antialiasing. Added vertical scroll. Add Functions.

:: Tested on windows 7 32bit

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off
if not "%~1" == "" goto %~1

:: title %~nx0

setlocal


:: variables

set /a lines=50, cols=120            &:: lines and columns for draw windows.


:: Double the resolution
:: For Hi-resolutions Lucida console 2! Undefine/empty Highres for disable or add/remove REM
rem set HighRes=ON            


:: draw

start "PGEN" "%~f0" :draw

goto :eof

:::::::::::::::::::::::::::::::::::
:draw


:: Setting windows size
If defined HighRes set /a lines*=2, cols*=2
cls & mode %cols%,%lines%

set /a "lines=lines/2-2, cols=cols/2-2, f=-1, zf=0"   &:: Adjust initial value for common variables


:: PI rounded for 4 Digit (Error=0.000007/8) , PI/2 , 2*PI and 3/4*PI
:: (PI=31416, PI/2=15708, 2*PI=62832 and 3/4PI=PI+PI/2)

:: Used best short approx of PI=355/113. See wikipedia...
set /a "PI=(35500000/113+5)/10, PI_div_2=(35500000/113/2+5)/10, PIx2=2*PI, PI34=PI+PI_div_2"


:: Redraw loop
:loop

If %random:~0,1% lss 5 (color 37) else color 5c      &:: Two color scroll


:: nf=number of functions, f=current function number

set /a "nf=6, lastf=nf-1, f=(f+1) %% nf"      &:: Automatic rotate functions

if %f% equ %lastf% set /a "zf=(zf+1) %% 4"      &:: Automatic rotate zoom


:: z1=zoom factor linear, z2=zoom f. exponential, s=sign

set /a "z1=zf , z2=z1*z1, s=(%random% %% 2)*2-1"

If defined HighRes (set /a "zH=2") else set /a "zH=1"   &:: For Hi-resolutions Lucida console 2!


:: Equations F(x). You can add!!

if %f% equ 4 set "Frm=( %%x * %%x / 2  +   %%y * %%y * 2 )   * (%z1%*512+20) / %zH%"
if %f% equ 0 set "Frm=( %%x * %%x / 2  -   %%y * %%y * 2 )   * (%z1%*512+ 8) / %zH%"
if %f% equ 2 set "Frm=( %%x * %%y / 2  +   %%y * %%x * 2 )   * (%z1%*512+ 8) / %zH%"
if %f% equ 3 set "Frm=( %%x * %%x / 2  ^ ~ %%y * %%y * 2 )   * (%z1%*48 +48) / %zH%"
if %f% equ 5 set "Frm=( %%x * %%x / 2  *   %%y * %%y * 2 )   * (%z1%*16+1)/8 / %zH%"
if %f% equ 1 set "Frm=( %%x * %%x / 2  /  (%%y * %%y * 2|1)) * (%z2%+2)*512  / %zH%"


:: Compute next pixel at the same time! SpeedUp 12% on High Resolution
:: set "FrmB=( (%%x+1) * (%%x+1) / 2  +   %%y * %%y * 2 ) * (%z1%*256+8) / %zH%"
setlocal enabledelayedexpansion
for /f "delims=" %%i in ("!Frm:%%x=(%%x+1)!") do (endlocal &set "FrmB=%%i")


::Trasparency : Merge Function with FunctionB in HighResolution work better.


:: Sin(x)*4+3
set "SIN=b*(a-a*a/1900*a/20*19/300000+(b=a*a/1900*a/15000*a/16000*a)/20*19/2500000-b/16000*a/16000*a/20*19/41015625)*4/10000+3"


:: For compute next pixel using variable c and d. This is not optimal. For the moment i leave in order to simplify the code
set "SIN2=%SIN:a=c%"
set "SIN2=%SIN2:b=d%"


:: Fast Compute and Draw
:: TO DO: Multicore usage for speed up the calculation and redraw using Fast methods [SORT+TYPE is faster]


::Empty environment for speeding the SET
(setlocal EnableDelayedExpansion & for /F "Tokens=1 delims==" %%v in ('set') do set "%%v="

  (
   :: Main cicle. Processes the rows
   For /L %%y in (-%lines%,1,%lines%) do (


     :: Divide compute by 2. For keep empty the environment in High resolution. Reduce environment size.
     For /L %%s in (0,1,1) do (


        :: Gradient/Ramp/Shadow/Pattern
        set "L=.:/°±²ÛÛ"


        :: Compute start and stop after dividing by 2 the calculation
        set /a "From=%%s*2-(1-%%s)*%cols%, To=%%s*%cols%"

   
        For /L %%x in (!From!,2,!To!) do (


          :: Fast Sin( F(x) ). calculation of two F(x) for speedup. Reduce number of call of SETs
          set /a "a=( %Frm% ) %% %PIx2%, a*=(b=(a>>31)*2+1), c=( %FrmB% ) %% %PIx2%, c*=(d=(c>>31)*2+1)"


          :: Pixel even
          if !a! geq %PI% set /a a-=%PI%, b=-b
          if !a! gtr %PI_div_2% (set /a "a=%PI%-a, a=%SIN%") else set /a "a=%SIN%"

          :: Pixel odd
          if !c! geq %PI% set /a c-=%PI%, d=-d
          if !c! gtr %PI_div_2% (set /a "c=%PI%-c, c=%SIN2%") else set /a "c=%SIN2%"


          :: Apply the gradient
          for /f "tokens=1,2" %%a in ("!a! !c!") do set L=!L!!L:~%%a,1!!L:~%%b,1!
        )


        :: Save the result on file to keep environment free
        if %%s equ 0 >%tmp%\pgen_l.$$.tmp echo( !L:~8!
     )


     :: Reload the var from file and merge. This is fast on HighResolution, but slow in low. I think an AUTOTUNING for next version maybe.
     <%tmp%\pgen_l.$$.tmp set /p "M="
     echo(!M!!L:~8!
     set M=


   :: Trick for speedup output.
   rem
   )) > CON:

endlocal)

echo(


:: Rotate Some colors

set HexC=087F192A3B4C5D6E
set Pause=OFF

For /L %%c in (1,1,15) do (
  call color 0%%HexC:~%%c,1%%
  If "%Pause%"=="ON" (pause>nul) else ping ::1 -n 2 >nul
)


goto :loop

exit

goto :eof
:::::::::::::::::::::::::::::::::::



PGEN version 0.1f:

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PGEN by Francesco Poscetti aka einstein1969 - 2014

:: For best view use Lucida Console 5 o less.

:: v. 0.1f Add parameter for run in High resolution Lucida console 2.
::         Tune speed. About 15% more speed in High resolution (tested @800x300)

:: v. 0.1e Added variable PI, PI2, 2PI for code readability and other comments.

:: v. 0.1d Patched gradient for better antialiasing. Added vertical scroll. Add Functions.

:: Tested on windows 7 32bit

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off & setlocal
if not "%~1" == "" goto %~1

title %~nx0


:: variables

set /a lines=70, cols=180      &:: lines and columns for draw windows.

:: Double the resolution
:: For Hi-resolutions Lucida console 2! Undefine/empty Highres for disable or add REM
rem set HighRes=ON

:: draw

start "PGEN" "%~f0" :draw

goto :eof

:::::::::::::::::::::::::::::::::::
:draw

If defined HighRes set /a lines*=2, cols*=2

cls & mode %cols%,%lines%

set /a "lines=lines/2-2, cols=cols/2-2, f=-1"


:: PI rounded for 4 Digit (Error=0.000007/8) , PI/2 , 2*PI and 3/4*PI
:: (PI=31416, PI/2=15708, 2*PI=62832 and 3/4PI=PI+PI/2)

:: Used best short approx of PI=355/113. See wikipedia...
set /a "PI=(35500000/113+5)/10, PI_div_2=(35500000/113/2+5)/10, PIx2=2*PI, PI34=PI+PI_div_2"


:: Redraw loop
:loop

If %random:~0,1% lss 5 (color 37) else color 5c      &:: Two color scroll


:: nf=number of functions, z=zoom factor, s=sign, f=function number

set /a "nf=6, z=(zf+0)*(zf+0)*256+7, s=(%random% %% 2)*2-1, f=(f+1) %% nf, lastf=nf-1"

If defined HighRes set /a z=z/3      &:: For Hi-resolutions Lucida console 2!

if %f% equ %lastf% set /a "zf=(zf+1) %% 4"   &:: Automatic rotate zoom


:: Equations F(x). You can add!!

if %f% equ 0 set "Frm=( %%x * %%x / 2  +   %%y * %%y * 2 ) * %z%"
if %f% equ 2 set "Frm=( %%x * %%x / 2  -   %%y * %%y * 2 ) * %z%"
if %f% equ 1 set "Frm=( %%x * %%y / 2  +   %%y * %%x * 2 ) * %z%"
if %f% equ 3 set "Frm=( %%x * %%x / 2  ^ ~ %%y * %%y * 2 ) * (%z%/48+4)"
if %f% equ 4 set "Frm=( %%x * %%x / 2  *   %%y * %%y * 2 ) * (%z%/256+1)/8"
if %f% equ 5 set "Frm=( %%x * %%x / 2  /  (%%y * %%y * 2 | 1) ) * ((%z%*%z%)|256)"


:: Sin(x)*4+3
set "SIN=b*(a-a*a/1900*a/20*19/300000+(b=a*a/1900*a/15000*a/16000*a)/20*19/2500000-b/16000*a/16000*a/20*19/41015625)*4/10000+3"


::Empty environment for speeding the SET
(setlocal EnableDelayedExpansion & for /F "Tokens=1 delims==" %%v in ('set') do set "%%v="

  (For /L %%y in (-%lines%,1,%lines%) do (

     For /L %%s in (0,1,1) do (

        :: Gradient/Ramp/Shadow/Pattern
        set "L=.:/°±²ÛÛ"

        set /a "From=%%s-(1-%%s)*%cols%, To=%%s*%cols%"
        For /L %%x in (!From!,1,!To!) do (

          :: Fast Sin( F(x) )
          set /a "a=( %Frm% ) %% %PIx2%, a*=(b=(a>>31)*2+1)"

          if !a! geq %PI% set /a a-=%PI%, b=-b
          if !a! gtr %PI_div_2% (set /a "a=%PI%-a, a=%SIN%") else set /a "a=%SIN%"


          :: Apply the gradient
          for /f %%a in ("!a!") do set L=!L!!L:~%%a,1!
        )

        if %%s equ 0 >%tmp%\pgen_l.$$.tmp echo( !L:~8!
     )

     <%tmp%\pgen_l.$$.tmp set /p "M="
     echo(!M!!L:~8!
     set M=

   )) > CON:

endlocal)

echo(

:: Rotate Some colors

set HexC=087F192A3B4C5D6E

For /L %%c in (1,1,15) do (call color 0%%HexC:~%%c,1%% & ping ::1 -n 2 >nul)

goto :loop

exit

goto :eof


EDIT : I have modified a little the demo for rotation of parameters and fix gradient and other staff. For version 0.1f.

einstein1969
Last edited by einstein1969 on 16 Apr 2014 08:47, edited 4 times in total.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Pgen [CodeSnippet]

#2 Post by einstein1969 » 12 Apr 2014 13:53

Hi, I have update with more comment, more speed and a parameter for high resolution. For example lucida console 2. Current version 0.1f

See the code for version history.

einstein1969

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pgen [CodeSnippet]

#3 Post by aGerman » 13 Apr 2014 04:43

Very impressive even if it isn't of any use for me.
There is something I don't understand:

Code: Select all

::Empty environment for speeding the SET
(setlocal EnableDelayedExpansion & for /F "Tokens=1 delims==" %%v in ('set') do set "%%v="


The leading parenthesis opens a block where normal environment variables are replaced with their values before the block is executed. So far I understood and that's the reason why it works anyway.
But why does emtying the environment speed up the SET commands? If the PATH variable is undefined you may increase the performance of the code because the command interpreter won't look for executable files in various places. (Somewhere we already discussed it ...) That could be the reason why the code is getting faster.
I think you should test it. Maybe the deletion of the entire environment again and again could be avoided.

Regards
aGerman

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Pgen [CodeSnippet]

#4 Post by einstein1969 » 13 Apr 2014 13:31

aGerman wrote:Very impressive even if it isn't of any use for me.
There is something I don't understand:

Code: Select all

::Empty environment for speeding the SET
(setlocal EnableDelayedExpansion & for /F "Tokens=1 delims==" %%v in ('set') do set "%%v="


The leading parenthesis opens a block where normal environment variables are replaced with their values before the block is executed. So far I understood and that's the reason why it works anyway.
But why does emtying the environment speed up the SET commands? If the PATH variable is undefined you may increase the performance of the code because the command interpreter won't look for executable files in various places. (Somewhere we already discussed it ...) That could be the reason why the code is getting faster.
I think you should test it. Maybe the deletion of the entire environment again and again could be avoided.

Regards
aGerman


Thanks aGerman.

The size of environment affect the speed of SET and SET /A. This is the link of discussion. Why does SET performance degrade as environment size grows?

The problem of the PATH is another problem.

I have done some test and the index of speed for pgen prior of v0.1f change on environment usage. Then i have used a trick that flush the env on disk and reload after calculating.

Resolution (Y*X) | Time | Index=time/orizzontal_resolution:

Code: Select all

300x010=  52cs 5,2
300x015=  75cs 5,0
300x020= 110cs 5,5
300x025= 140cs 5,6
300x050= 290cs 5,8
300x100= 615cs 6,2
300x150= 945cs 6,3
300x200=1290cs 6,5
300x300=2060cs 6,9
300x400=2900cs 7,3
300x600=4680cs 7,8
300x800=6800cs 8,5

The resolution orizzontal is proportional at the env used.



There are other aspect that affect the SET /A.

I have update the main thread and added the new version that is about 12% more speed on high resolution.

The other aspect is the number of variables used and the position on the environment.

version 0.1g is out. See the version history in the code for the update.

I have added other comments.

Thanks for the tip for the repeat deletion of environment. I think if this speed up... :D


einstein1969

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Pgen [CodeSnippet]

#5 Post by Aacini » 15 Apr 2014 13:28

I like the output of this program, so I wanted to modify it to run faster and at higher resolutions. However, in order to modify a program we need to understand it first, at least at a degree enough for don't crash it! The original code is full of details that just made it difficult to understand, like add 0 to a variable, define variables that are not used, use a twisted and tangled method to generate output lines, etc. This way, I had to start modifying the code just to increase its legibility:

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PGEN by Francesco Poscetti aka einstein1969 - 2014

:: For best view use Lucida Console 5 o less.

:: v. 0.1f Add parameter for run in High resolution Lucida console 2.
::         Tune speed. About 15% more speed in High resolution (tested @800x300)

:: v. 0.1e Added variable PI, PI2, 2PI for code readability and other comments.

:: v. 0.1d Patched gradient for better antialiasing. Added vertical scroll. Add Functions.

:: Tested on windows 7 32bit

@rem Modified to improve legibility by Antonio Perez Ayala (aka Aacini)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off & setlocal

:: variables
set /a lines=70, cols=180      &:: lines and columns for draw windows.

:: Double the resolution
:: For Hi-resolutions Lucida console 2! Undefine/empty Highres for disable or add REM
rem set HighRes=ON
If defined HighRes set /a lines*=2, cols*=2
cls & mode %cols%,%lines% & echo/
set /a "lines=lines/2-2, cols=cols/2-2, f=-1"

:: PI rounded for 4 Digit (Error=0.000007/8) , PI/2 , 2*PI and 3/4*PI
:: (PI=31416, PI/2=15708, 2*PI=62832 and 3/4PI=PI+PI/2)
:: Used best short approx of PI=355/113. See wikipedia...
set /a "PI=(35500000/113+5)/10, PI_div_2=(35500000/113/2+5)/10, PIx2=2*PI, PI34=PI+PI_div_2"

:: Redraw loop
:loop

If %random:~0,1% lss 5 (color 37) else color 5c      &:: Two color scroll

:: nf=number of functions, z=zoom factor, f=function number
set /a "nf=6, z=zf*zf*256+7, f=(f+1) %% nf, lastf=nf-1"
If defined HighRes set /a z=z/3      &:: For Hi-resolutions Lucida console 2!
if %f% equ %lastf% set /a "zf=(zf+1) %% 4"   &:: Automatic rotate zoom

:: Equations F(x,y). You can add!!
if %f% equ 0 set "Frm=( %%x * %%x / 2  +   %%y * %%y * 2 ) * %z%"
if %f% equ 1 set "Frm=( %%x * %%y / 2  +   %%y * %%x * 2 ) * %z%"
if %f% equ 2 set "Frm=( %%x * %%x / 2  -   %%y * %%y * 2 ) * %z%"
if %f% equ 3 set "Frm=( %%x * %%x / 2  ^ ~ %%y * %%y * 2 ) * (%z%/48+4)"
if %f% equ 4 set "Frm=( %%x * %%x / 2  *   %%y * %%y * 2 ) * (%z%/256+1)/8"
if %f% equ 5 set "Frm=( %%x * %%x / 2  /  (%%y * %%y * 2 | 1) ) * ((%z%*%z%)|256)"
title %f%-   f(x,y) = "%Frm%"

:: Sin(x)*4+3
set "SIN=b*(a-a*a/1900*a/20*19/300000+ (b=a*a/1900*a/15000*a/16000*a) /20*19/2500000-b/16000*a/16000*a/20*19/41015625)*4/10000+3"

::Empty environment for speeding the SET
( rem Start block to preserve deleted %variables%
   setlocal EnableDelayedExpansion
   for /F "Tokens=1 delims==" %%v in ('set') do set "%%v="

   :: Gradient/Ramp/Shadow/Pattern
   set "L=.:/°±²ÛÛ"

   (For /L %%y in (-%lines%,1,%lines%) do (

      set "M="
      For /L %%x in (-%cols%,1,%cols%) do (

         :: Fast Sin( F(x) )
         set /a "a=( %Frm% ) %% %PIx2%, a*=(b=(a>>31)*2+1)"
         if !a! geq %PI% set /a a-=%PI%, b=-b
         if !a! gtr %PI_div_2% set /a a=%PI%-a
         set /a "a=%SIN%"

         :: Apply the gradient
         for /f %%a in ("!a!") do set "M=!M!!L:~%%a,1!"

      )
      echo  !M!

   )) > CON:

   endlocal
)
echo/

:: Rotate Some colors
set HexC=087F192A3B4C5D6E
For /L %%c in (1,1,15) do (call color 0%%HexC:~%%c,1%% & ping ::1 -n 2 >nul)

goto :loop

Then I modified previous code so the X and Y loops and the calculations are made in JScript, and also show the output in several colors via my ColorChar.exe auxiliary program:

Code: Select all

@if (@CodeSection == @Batch) @then

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PGEN by Francesco Poscetti aka einstein1969 - 2014

:: For best view use Lucida Console 5 o less.

:: v. 0.1f Add parameter for run in High resolution Lucida console 2.
::         Tune speed. About 15% more speed in High resolution (tested @800x300)

:: v. 0.1e Added variable PI, PI2, 2PI for code readability and other comments.

:: v. 0.1d Patched gradient for better antialiasing. Added vertical scroll. Add Functions.

:: Tested on windows 7 32bit

@rem Batch-JScript hybrid version by Antonio Perez Ayala (aka Aacini)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off & setlocal
if "%~1" equ "DrawLines" goto DrawLines

:: variables
set /a lines=70, cols=180      &:: lines and columns for draw windows.

:: Double the resolution
:: For Hi-resolutions Lucida console 2! Undefine/empty Highres for disable or add REM
set HighRes=ON
If defined HighRes set /a lines=384, cols=512
mode %cols%,%lines%
set /a "lines=lines/2-2, cols=cols/2-2, f=-1"

:: PI rounded for 4 Digit (Error=0.000007/8) , PI/2 , 2*PI and 3/4*PI
:: (PI=31416, PI/2=15708, 2*PI=62832 and 3/4PI=PI+PI/2)
:: Used best short approx of PI=355/113. See wikipedia...
set /a "PI=(35500000/113+5)/10, PI_div_2=(35500000/113/2+5)/10, PIx2=2*PI, PI34=PI+PI_div_2"

:: Redraw loop
:loop

:: nf=number of functions, z=zoom factor, f=function number
set /a "nf=6, z=zf*zf*256+7, f=(f+1) %% nf, lastf=nf-1"
If defined HighRes set /a z=z/3      &:: For Hi-resolutions Lucida console 2!
if %f% equ %lastf% set /a "zf=(zf+1) %% 4"   &:: Automatic rotate zoom

:: Equations F(x,y). You can add!!
if %f% equ 0 set "Frm=( x * x / 2  +   y * y * 2 ) * %z%"
if %f% equ 1 set "Frm=( x * y / 2  +   y * x * 2 ) * %z%"
if %f% equ 2 set "Frm=( x * x / 2  -   y * y * 2 ) * %z%"
if %f% equ 3 set "Frm=( x * x / 2  ^ ~ y * y * 2 ) * (%z%/48+4)"
if %f% equ 4 set "Frm=( x * x / 2  *   y * y * 2 ) * (%z%/256+1)/8"
if %f% equ 5 set "Frm=( x * x / 2  /  (y * y * 2 | 1) ) * ((%z%*%z%)|256)"
title %f%-  f(x,y) = "%Frm%"
cls
echo/

:: Sin(x)*4+3
set "SIN=b*(a-a*a/1900*a/20*19/300000+ (b=a*a/1900*a/15000*a/16000*a) /20*19/2500000-b/16000*a/16000*a/20*19/41015625)*4/10000+3"

:: Gradient/Ramp/Shadow/Pattern (colors in this case)
set "L=145879BF"


rem Create the semaphore-signal file and start the asynchronous process
echo X > Flag.out
if exist Flag.in del Flag.in
rem Get output lines from JScript section and show they with ColorChar.exe
CScript //nologo //E:JScript "%~F0" | "%~F0" DrawLines
del Flag.out
pause

goto :loop


:DrawLines
   rem Wait for "Data Available" signal
   if not exist Flag.in goto DrawLines
   rem Read the input line sent by JScript code
   set line=
   set /P "line="
   rem Set "Data Read" acknowledgement
   ren Flag.in Flag.out
   rem Check the standard "End Of piped File" mark
   if not defined line goto :EOF
   ColorChar %line% 13 10
goto DrawLines


@end


var fso = new ActiveXObject("Scripting.FileSystemObject"),
    EnvVar = WScript.CreateObject("WScript.Shell").Environment("PROCESS"),
    lines = parseInt(EnvVar("lines")), cols = parseInt(EnvVar("cols")),
    PI = parseInt(EnvVar("PI")), PI_div_2 = parseInt(EnvVar("PI_div_2")), PIx2 = parseInt(EnvVar("PIx2")),
    Frm = EnvVar("Frm"), SIN = EnvVar("SIN"), L = EnvVar("L");

for ( var y = -lines; y <= lines; y++ ) {

   var M = "32", a, b, prevA = -1, count = 0;
   for ( var x = -cols; x <= cols; x++ ) {

      // Fast Sin( F(x) )
      a = Math.floor( eval(Frm) ) % PIx2, a*=(b=(a>>31)*2+1);
      if ( a >= PI ) a-=PI, b=-b;
      if ( a >  PI_div_2 ) a=PI-a;
      a = Math.floor( eval(SIN) );

      // Apply the gradient
      if ( a != prevA ) {
         if (count) M += " /"+L.substr(prevA,1) + " 219" + ((count>1)?("*"+count):"");
         prevA = a;
         count = 1;
      } else {
         count++;
      }

   }
   M += " /"+L.substr(prevA,1) + " 219" + ((count>1)?("*"+count):"");
   sendLine(M);

}

// Wait for last "Data Read" acknowledgement
while ( ! fso.FileExists("Flag.out") ) {
      WScript.Sleep(1);
}
// Send the standard "End Of piped File" mark
WScript.Echo();
fso.MoveFile("Flag.out", "Flag.in");


function sendLine ( line ) {
   // Wait for "Data Read" acknowledgement
   while ( ! fso.FileExists("Flag.out") ) {
      WScript.Sleep(1);
   }
   // Send the line to Batch code
   WScript.Echo(line);
   // Set "Data Available" signal
   fso.MoveFile("Flag.out", "Flag.in");
}

This program produce a very interesting output; the following images were shown using the 1x1 font @ 512x384 resolution:

Image

Image

However, in other equations this program produce wrong images or even crash; this behaviour is obviously produced by JScript's floating point operations instead of Batch integer ones. At first, I tried to duplicate all Batch integer operations in JScript via Math.floor function on division results and other tricks, but I failed. Then, I tried to completely eliminate the integer operations and just use "Math.sin( (eval(Frm)) % (Math.PI*2) ) * 4 + 3", but failed again. Of course, this happened because I know nothing about this program. I don't understand the method used, nor the values used, nor why Sin function is used, etc. I think I could do some research on this subject and I am pretty sure that I eventually solve it, but I want not to waste my time again in a matter I really are not interested in...

I think that all code snippets posted on this forum that are not about the usual topics treated here should include a brief description on the subject and one or two further links if the matter is complex enough...

Antonio

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Pgen 0.1g [DemoBatch]

#6 Post by Aacini » 17 Apr 2014 23:34

After several tests I was able to get the base method that allows to evaluate the equations and directly use JScript's Math.sin function on them:

Code: Select all

@if (@CodeSection == @Batch) @then

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PGEN by Francesco Poscetti aka einstein1969 - 2014

:: For best view use Lucida Console 5 o less.

:: v. 0.1f Add parameter for run in High resolution Lucida console 2.
::         Tune speed. About 15% more speed in High resolution (tested @800x300)

:: v. 0.1e Added variable PI, PI2, 2PI for code readability and other comments.

:: v. 0.1d Patched gradient for better antialiasing. Added vertical scroll. Add Functions.

:: Tested on windows 7 32bit

@rem Batch-JScript hybrid version by Antonio Perez Ayala (aka Aacini)
@rem The best view is using 1x1 (one pixel size) raster font

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off
setlocal
if "%~1" equ "DrawLines" goto DrawLines

:: variables
set /a lines=70, cols=180      &:: lines and columns for draw windows.

:: Double the resolution
:: For Hi-resolutions Lucida console 2! Undefine/empty Highres for disable or add REM
set HighRes=ON
if defined HighRes set /a lines=400, cols=400
mode %cols%,%lines%
set /a "lines=lines/2-2, cols=cols/2-2, f=-1, colors=4"

:: Gradient/Ramp/Shadow/Pattern (colors in this case)
set "L=145879BF"

:: Redraw loop
:loop

:: nf=number of functions, z=zoom factor, f=function number
set /a "nf=6, z=zf*zf*256+7, f=(f+1) %% nf, lastf=nf-1"
if defined HighRes set /a z=z/3      &:: For Hi-resolutions Lucida console 2!
if %f% equ %lastf% (
   set /a "zf=(zf+1) %% 4"   &:: Automatic rotate zoom
   set colors=7
   set "L=123456879ABCDEF"
)

:: Equations F(x,y). You can add!!
if %f% equ 0 set "Frm=( x * x / 2  +   y * y * 2 ) * %z%"
if %f% equ 1 set "Frm=( x * y / 2  +   y * x * 2 ) * %z%"
if %f% equ 2 set "Frm=( x * x / 2  -   y * y * 2 ) * %z%"
if %f% equ 3 set "Frm=( x * x / 2  ^ ~ y * y * 2 ) * (%z%/48+4)"
if %f% equ 4 set "Frm=( x * x / 2  *   y * y * 2 ) * (%z%/256+1)/8"
if %f% equ 5 set "Frm=( x * x / 2  /  (y * y * 2 | 1) ) * ((%z%*%z%)|256)"
title f%f%="%Frm%"
cls
echo/


rem Create the semaphore-signal file and start the asynchronous process
echo X > Flag.out
if exist Flag.in del Flag.in
rem Get output lines from JScript section and show they with ColorChar.exe
CScript //nologo //E:JScript "%~F0" | "%~F0" DrawLines
del Flag.out
pause

goto :loop


:DrawLines
   rem Wait for "Data Available" signal
   if not exist Flag.in goto DrawLines
   rem Read the input line sent by JScript code
   set "line="
   set /P "line="
   rem Set "Data Read" acknowledgement
   ren Flag.in Flag.out
   rem Check the standard "End Of piped File" mark
   if not defined line goto :EOF
   ColorChar %line%
goto DrawLines


@end


var fso = new ActiveXObject("Scripting.FileSystemObject"),
    EnvVar = WScript.CreateObject("WScript.Shell").Environment("PROCESS"),
    lines = parseInt(EnvVar("lines")), cols = parseInt(EnvVar("cols")),
    PI = Math.PI, PI_div_2 = PI/2, PIx2 = PI*2,
    Frm = EnvVar("Frm"), L = EnvVar("L"), colors = EnvVar("colors"), color2 = (colors==4)?3:7;

for ( var y = -lines; y <= lines; y++ ) {

   var M = "32", a, prevA = -1, count = 0;
   for ( var x = -cols; x <= cols; x++ ) {

      // Sin( F(x) )
      a = Math.floor( Math.sin((eval(Frm)/10000) % PIx2)*colors+color2 );

      // Apply the gradient
      if ( a != prevA ) {
         if ( count ) M += " /"+L.substr(prevA,1) + " 219" + ((count>1)?("*"+count):"");
         if ( M.length > 1000 ) { sendLine(M); M = ""; }
         prevA = a;
         count = 1;
      } else {
         count++;
      }

   }
   M += " /"+L.substr(prevA,1) + " 219" + ((count>1)?("*"+count):"");
   sendLine(M+" 13 10");

}

// Wait for last "Data Read" acknowledgement
while ( ! fso.FileExists("Flag.out") ) {
      WScript.Sleep(1);
}
// Send the standard "End Of piped File" mark
WScript.Stdout.WriteLine();
fso.MoveFile("Flag.out", "Flag.in");


function sendLine ( line ) {
   // Wait for "Data Read" acknowledgement
   while ( ! fso.FileExists("Flag.out") ) {
      WScript.Sleep(1);
   }
   // Send the line to Batch code
   WScript.Stdout.WriteLine(line);
   // Set "Data Available" signal
   fso.MoveFile("Flag.out", "Flag.in");
}

The output of this program is amazing! The following images were created with the 1x1 raster font at a resolution of 400x400:

Image Image

Image Image

Antonio

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Pgen 0.1g [DemoBatch]

#7 Post by einstein1969 » 20 May 2014 16:09

@Antonio!

those colors you chose are great!

It 'a pleasure to watch. Good job! 8)

@All

Do you ever make a mistake in the code and have a nice effect?

Well I made the mistake!

Guess where is the error! :D

Code: Select all

@echo off & setlocal EnableDelayedExpansion

rem font raster 8x8

mode 81,70

color 0a

set "G= ú.:/°±²Û  ú..:://°°±±²²ÛÛ"

set n=0
for /L %%y in (-33,1,33) do (
  set /a n+=1
  for /L %%x in (-39,1,39) do (
    set /a "f=(%%x*%%x-%%y*%%y)/8 %% 9"
    for %%f in (!f!) do for %%n in (!n!) do set L%%n=!L%%n!!G:~%%f,1!
  )
)

pause

set i=0
for /L %%n in (1,1,100000) do (

  if not "!OT!"=="!time:~10,1!" (

    set /a "c=i %% 2, i+=1"
    if !c! equ 0 (
      cls
      For /L %%y in (1,1,!n!) do echo( !L%%y!

      For /L %%y in (1,1,!n!) do (
 
        set L%%y=!L%%y: =P!
        set L%%y=!L%%y:ú= !
        set L%%y=!L%%y:.=ú!
        set L%%y=!L%%y::=.!
        set L%%y=!L%%y:/=:!
        set L%%y=!L%%y:°=/!
        set L%%y=!L%%y:±=°!
        set L%%y=!L%%y:²=±!
        set L%%y=!L%%y:Û=²!
        set L%%y=!L%%y:P=Û!

      )
    )
    set OT=!time:~10,1!
  )
)


I will add in the next version of Pgen!

EDIT: If it is too fast you can change this line of code:

Code: Select all

    set /a "c=i %% 2, i+=1"

changing the 2 with 4 or 8 or greater.

einstein1969

Post Reply