I want to open this thread for two reasons:
1 - gather here the various techniques that are scattered around to manage the cmd window.
2 - solve a problem regarding the aspect ratio for drawing shapes using the new escape sequences of windows 10
I make an example as short as possible. The famous circle.
Here I have used almost none of the techniques that are out there on the web and here.
Code: Select all
@echo off & setlocal enableDelayedExpansion & if NOT "%1"=="" goto :subs
rem save this script in UTF-8
rem multithread
Start "" "%0" Red
Start "" "%0" Green
Start "" "%0" Blue
Start "" "%0" Yellow
Start "" "%0" Cyan
Start "" "%0" Magenta
Start "" "%0" White
Start "" "%0" Orange
Start "" "%0" Pink
Start "" "%0" Salmon
Start "" "%0" Navy
Start "" "%0" Gray
goto :eof
:subs
title %1
call :init
rem setting amount of char 50x50
set /A img.x=50, img.y=50
rem font size in pixels, change this for respect aspect ratio.
set /A xc=8, yc=16
set /A width=img.x*xc, height=img.y*yc
rem using aspect ratio for setting windows size
if !width! leq !height! (
set /a res=width, img.y=img.y*xc/yc
) else set /a res=height, img.x=img.x*yc/xc
mode CON: COLS=!img.x! LINES=!img.y!
rem draw a circle (x-x0)^2+(y-y0)^2=r^2 . Implicit equation for simpler math.
rem set radius and center in cmd windows
set /A "R=res/2, X0=res/2, Y0=res/2"
rem setting step for better smoothing colors
set /A "mS=-(R*R), step=255*1000/-mS, stepL=128*1000/-mS"
For /L %%y in (1,1,!img.y!) do (
For /L %%x in (1,1,!img.x!) do (
rem calculate circle equation and color for smooting.
rem This code generate multiple circlesof different color and simulate a 3D sphere.
set /A px=%%x*xc, py=%%y*yc, x=px-x0, y=py-y0, S=y*y+x*x-R*R, C=-S*step/1000, CL=-S*stepL/1000"
if !S! leq 0 (
if "%1"=="Red" %plot% %%x %%y !C! 0 0
if "%1"=="Green" %plot% %%x %%y 0 !C! 0
if "%1"=="Blue" %plot% %%x %%y 0 0 !C!
if "%1"=="Yellow" %plot% %%x %%y !C! !C! 0
if "%1"=="Cyan" %plot% %%x %%y 0 !C! !C!
if "%1"=="Magenta" %plot% %%x %%y !C! 0 !C!
if "%1"=="White" %plot% %%x %%y !C! !C! !C!
if "%1"=="Orange" %plot% %%x %%y !C! !CL! 0
if "%1"=="Pink" %plot% %%x %%y !C! 0 !CL!
if "%1"=="Salmon" %plot% %%x %%y !C! !CL! !CL!
if "%1"=="Navy" %plot% %%x %%y 0 0 !CL!
if "%1"=="Gray" %plot% %%x %%y !CL! !CL! !CL!
)
)
)
%flush%
pause>nul
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:init
rem utf8
chcp 65001 >nul
rem for ansi sequence
for /F %%a in ('echo prompt $E^| %ComSpec%') do set "ESC=%%a"
rem clear environment for faster execution of SET command
set "Path=%SystemRoot%\system32"
for /F "Tokens=1 delims==" %%v in ('set') do if not %%v==ESC if not %%v==TMP if not %%v==Path set "%%v="
:: Hide the cursor
<nul set /p "=!ESC![?25l"
rem ALT+219
set "Char=█"
rem set "Char=*"
rem macro
:: define LF as a Line Feed (newline) character
set ^"LF=^
^" Above empty line is required - do not remove
:: define a newline with line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: macro Plot=Screen.Setpixel X Y R G B
set "Buffer="
set Plot=for %%# in (1 2) do if %%#==2 (%\n%
for /f "tokens=1-5 delims=, " %%1 in ("^!args^!") do ( %\n%
if not defined Buffer (%\n%
set "Buffer=^!ESC^![%%2;%%1H^!ESC^![38;2;%%3;%%4;%%5m^!Char^!" %\n%
) else ( %\n%
if "^!Buffer:~7000,1^!"=="" ( %\n%
set "Buffer=^!Buffer^!^!ESC^![%%2;%%1H^!ESC^![38;2;%%3;%%4;%%5m^!Char^!" %\n%
) else ( %\n%
^<nul set /p "=^!Buffer^!^!ESC^![%%2;%%1H^!ESC^![38;2;%%3;%%4;%%5m^!Char^!^!ESC^![0m" %\n%
set "Buffer=" %\n%
) %\n%
) %\n%
)) else set args=
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Flush flush the variable buffer.
set Flush=^<nul set /p "=^!Buffer^!^!ESC^![0m" ^& set "Buffer="
goto :eof
This is because the cmd window fonts are not set as the script wants them.
In particular, the line that manages the size of the characters must be modified accordingly to the type of font that the cmd window is using. You can do this and relaunch the program and i the ellipse should become circles.
Code: Select all
rem font size in pixels, change this for respect aspect ratio.
set /A xc=8, yc=16
The screen of my new laptop is 1920x1080 (FullHD) and in the screen settings if I put a recommended resizing of 125% it messes up all the work I have to do to recalculate the new aspect ratio. But I don't know where to get the data
Place the two images.
with 100% ----------------------------------------------------> with 125% (are ellipsoids)
3 - PS. Can any expert tell me the latest syntax to define the %\n% for macros?
Einstein1969