3D points to 2D projection

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
IcarusLives
Posts: 181
Joined: 17 Jan 2016 23:55

3D points to 2D projection

#1 Post by IcarusLives » 07 May 2019 22:04

Hello! I have been experimenting with 3D points, and I am seeking further knowledge!

If anyone can lead me to some valuable threads or sites I would very much appreciate it! Thanks in advanced :)

Image

Code: Select all

@echo off & setlocal enableDelayedExpansion

call :macros

for /l %%# in () do ( for /l %%s in (1,1,%stars%) do (
		
		if !z[%%s]! lss 1 (
			set /a "x[%%s]=!random! %% wid", "y[%%s]=!random! %% hei", "z[%%s]=wid"
			set /a "pz[%%s]=z[%%s]"
		)
		
		2>nul set /a ^
			"z[%%s]-=speed",^
			"sx=(wid/2)+((x[%%s]-(wid/4))-(wid/2))*((((x[%%s]*100)/z[%%s])/10)-(0))/((100)-(0))",^
			"sy=(hei/2)+((y[%%s]-(hei/4))-(hei/2))*((((y[%%s]*100)/z[%%s])/10)-(0))/((100)-(0))",^
			"px=(wid/2)+((x[%%s]-(wid/4))-(wid/2))*((((x[%%s]*100)/pz[%%s])/10)-(0))/((100)-(0))",^
			"py=(hei/2)+((y[%%s]-(hei/4))-(hei/2))*((((y[%%s]*100)/pz[%%s])/10)-(0))/((100)-(0))",^
			"pz[%%s]=z[%%s]"
			
		%line% px py sx sy %%s
		set "screen=!screen!!$line!"
		
	)
	<nul set /p "=%esc%[2J!screen!" & set "screen="
)

:macros
(set \n=^^^
%= This creates an escaped Line Feed - DO NOT ALTER =%
)
for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
<nul set /p "=!esc![?25l"

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"
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"
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"


set /a "hei=wid=100"
mode %wid%,%hei%

set /a "stars=5", "speed=4"
for /l %%a in (1,1,%stars%) do (
	set /a "x[%%a]=!random! %% wid", "y[%%a]=!random! %% hei", "z[%%a]=!random! %% (wid/2) + wid"
	set /a "pz[%%a]=z[%%a]"
)

set line=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-5" %%1 in ("^!args^!") do (%\n%
	if "%%~5" equ "" ( set "hue=255" ) else ( set "hue=%%~5")%\n%
	set "$line=%esc%[38;5;^!hue^!m"%\n%
	set /a "xa=%%~1", "ya=%%~2", "xb=%%~3", "yb=%%~4", "dx=%%~3 - %%~1", "dy=%%~4 - %%~2"%\n%
	if ^^!dy^^! lss 0 ( set /a "dy=-dy", "stepy=-1" ) else ( set "stepy=1" )%\n%
	if ^^!dx^^! lss 0 ( set /a "dx=-dx", "stepx=-1" ) else ( set "stepx=1" )%\n%
	set /a "dx<<=1", "dy<<=1"%\n%
	if ^^!dx^^! gtr ^^!dy^^! (%\n%
		set /a "fraction=dy - (dx >> 1)"%\n%
		for /l %%x in (^^!xa^^!,^^!stepx^^!,^^!xb^^!) do (%\n%
			if ^^!fraction^^! geq 0 set /a "ya+=stepy", "fraction-=dx"%\n%
			set /a "fraction+=dy"%\n%
			set "$line=^!$line^!%esc%[^!ya^!;%%xHÛ"%\n%
		)%\n%
	) else (%\n%
		set /a "fraction=dx - (dy >> 1)"%\n%
		for /l %%y in (^^!ya^^!,^^!stepy^^!,^^!yb^^!) do (%\n%
			if ^^!fraction^^! geq 0 set /a "xa+=stepx", "fraction-=dy"%\n%
			set /a "fraction+=dx"%\n%
			set "$line=^!$line^!%esc%[%%y;^!xa^!HÛ"%\n%
		)%\n%
	)%\n%
)) else set args=
goto :eof
Image

Code: Select all

@echo off & setlocal enableDelayedExpansion & call :macros

rem loop forever
for /l %%# in () do ( 

	rem for all star objects
	for /l %%s in (1,1,%stars%) do (
		
		rem if object Z is too close to "us" then reset it
		if !z[%%s]! lss 1 set /a "x[%%s]=!random! %% wid", "y[%%s]=!random! %% hei", "z[%%s]=wid"
		REM if !z[%%s]! lss 1 set /a "x[1]=50","y[1]=50","z[1]=wid","x[2]=40","y[2]=50","z[2]=wid","x[3]=60","y[3]=50","z[3]=wid"
		
		rem calculate/map transformed X Y and Z axis into 2D space - SX SY. Size of object - R
		2>nul set /a ^
			"z[%%s]-=speed",^
			"v=((x[%%s]*100)/z[%%s])/10", "a=0", "b=100", "c=x[%%s]", "d=wid/2", "%map:?=sx%",^
			"v=((y[%%s]*100)/z[%%s])/10", "a=0", "b=100", "c=y[%%s]", "d=hei/2", "%map:?=sy%",^
			"v=z[%%s]", "a=0", "b=wid/2", "c=4", "d=2", "%map:?=r%"
		
		rem draw ball object at SX,SY with a size of R
		set "$ball=%esc%[38;5;%%sm" & for /l %%c in (0,90,360) do (
			set /a "cx=r * !cos:x=%%c! + sx", "cy=r * !sin:x=%%c! + sy"
			set "$ball=!$ball!%esc%[!cy!;!cx!HÛ"
		)
		
		rem draw ball to screen
		set "screen=!screen!!$ball!"	
	)
	rem display screen
	<nul set /p "=%esc%[2J!screen!" & set "screen="
)

:macros
(set \n=^^^
%= This creates an escaped Line Feed - DO NOT ALTER =%
)
( for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a" ) & <nul set /p "=!esc![?25l"

rem define MAP, SIN, COS
set "map=?=(c)+((d)-(c))*((v)-(a))/((b)-(a))"
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"
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"
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"

set /a "hei=wid=100"
mode %wid%,%hei%

set /a "stars=3", "speed=2"
for /l %%a in (1,1,%stars%) do (
	set /a "x[%%a]=!random! %% wid", "y[%%a]=!random! %% hei", "z[%%a]=!random! %% wid"
)

REM set /a "x[1]=50","y[1]=50","z[1]=wid","x[2]=40","y[2]=50","z[2]=wid","x[3]=60","y[3]=50","z[3]=wid"

goto :eof

IcarusLives
Posts: 181
Joined: 17 Jan 2016 23:55

Re: 3D points to 2D projection

#2 Post by IcarusLives » 21 Mar 2025 19:17

I have revisited this project.

Code: Select all

@echo off & setlocal enableDelayedExpansion

call :Set_Font "lucida console" 2 nomax %1 || exit

call :init

call :constructStars

for /l %%# in () do (

	for /l %%i in (1,1,%stars%) do (
	
		set /a "tz[%%i]-=speed[%%i]"
		
		if !tz[%%i]! lss 1 (
			set /a "x=-wid, y=wid, tx[%%i]=%rnd(x,y)%",^
				   "x=-hei, y=hei, ty[%%i]=%rnd(x,y)%",^
				                  "tz[%%i]=wid",^
				   "x=10, y=40, speed[%%i]=%rnd(x,y)%",^
				   "color[%%i]=!random! %% 14"
		)
		
		set /a "sx[%%i]=(wid/2) + range * tx[%%i] / tz[%%i]",^
		       "sy[%%i]=(hei/2) + range * ty[%%i] / tz[%%i]",^
			   "r=15 + -15 * tz[%%i] / wid"
		
		if !sx[%%i]! gtr 0 if !sx[%%i]! lss %wid% if !sy[%%i]! gtr 0 if !sy[%%i]! lss %hei% for %%r in (!r!) do (
			set "field=!field!%\e%[48;5;!color[%%i]!m%\e%[!sy[%%i]!;!sx[%%i]!H!circle[%%r]!"
		)
		
	)

	echo %\e%[2J!field!%\e%[m
	set "field="
)



:Set_Font FontName FontSize max/nomax dummy
if "%4"=="" (
	for /f "tokens=1,2 delims=x" %%a in ("%~2") do if "%%b"=="" (set /a "FontSize=%~2*65536") else set /a "FontSize=%%a+%%b*65536"
	reg add "HKCU\Console\%~nx0" /v FontSize /t reg_dword /d !FontSize! /f
	reg add "HKCU\Console\%~nx0" /v FaceName /t reg_sz /d "%~1" /f
	set "m=" & if /I "%~3"=="max" set "m=/max"
	start "%~nx0" !m! "%ComSpec%" /c "%~f0" _ 
	exit /b 1
) else ( >nul reg delete "HKCU\Console\%~nx0" /f )
goto:eof



:init
rem Empty environment, but keep some essentials
for /f "tokens=1 delims==" %%a in ('set') do (
	set "pre=true"
	for %%b in (cd Path ComSpec SystemRoot temp windir) do (
		if /i "%%a" equ "%%b" set "pre="
	)
	if defined pre set "%%~a="
)
set "pre="

(set \n=^^^
%= This creates an escaped Line Feed - DO NOT ALTER       \n =%
)

for /f %%a in ('echo prompt $E^| cmd') do set "\e=%%a" %= \e =%

set /a "wid=600, hei=300"
mode %wid%,%hei%


set "rnd(x,y)=(((^!random^! * 32768 + ^!random^!) %% (y - x + 1)) + x)"


rem %@fillCircle% radius color <rtn> !$fillCircle!
set @fillCircle=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1,2" %%1 in ("^!args^!") do (%\n%
	if "%%~2" neq "" ( set "$fillCircle=%\e%[48;5;%%~2m" ) else ( set "$fillCircle=%\e%[48;5;15m" )%\n%
	set /a "$rr=%%~1 * %%~1", "hr=%%~1 - 1"%\n%
	for /l %%y in (-%%~1,1,%%~1) do (%\n%
		set "$sx=0"%\n%
		for /l %%x in (-%%~1,1,%%~1) do (%\n%
			set /a "$xxyy=%%x*%%x+%%y*%%y"%\n%
			if ^^!$xxyy^^! lss ^^!$rr^^! set /a "$sx+=1"%\n%
		)%\n%
		if ^^!$sx^^! gtr 0 (%\n%
			set /a "shift=lx / 2 - $sx / 2"%\n%
			if ^^!shift^^! leq -1 (%\n%
				set "shift=%\e%[^!shift:-=^!D"%\n%
				set "shift=^!shift:1=^!"%\n%
			) else if ^^!shift^^! geq 1 (%\n%
				set "shift=%\e%[^!shift^!C"%\n%
				set "shift=^!shift:1=^!"%\n%
			) else (%\n%
				set "shift="%\n%
			)%\n%
			set "$fillCircle=^!$fillCircle^!^!shift^!%\e%[^!$sx^!X%\e%[B"%\n%
			set /a "lx=$sx"%\n%
		)%\n%
	)%\n%
	set "$fillCircle=%\e%[^!hr^!A^!$fillCircle^!%\e%[0m"%\n%
)) else set args=
goto :eof



:constructStars
set "stars=20"

set "range=200"

set /a "x=10, y=40, speed=%rnd(x,y)%"

for /l %%i in (1,1,%stars%) do (
	set /a "x=-wid, y=wid, tx[%%i]=%rnd(x,y)%"
	set /a "x=-hei, y=hei, ty[%%i]=%rnd(x,y)%"
	set /a "x=0,    y=wid, tz[%%i]=%rnd(x,y)%"
	set /a "x=10, y=40, speed[%%i]=%rnd(x,y)%"
	set /a "color[%%i]=!random! %% 14"
)




for /l %%i in (1,1,15) do (
	%@fillCircle% %%i
	set "circle[%%i]=!$fillCircle:%\e%[48;5;15m=!"
)
goto :eof
Image

Post Reply