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