Page 1 of 1

3D rotating donut

Posted: 15 Mar 2025 06:02
by IcarusLives
Image

Code: Select all

@echo off & setlocal enableDelayedExpansion

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

for /f %%a in ('echo prompt $E^| cmd') do set "\e=%%a" %= \e =%
echo %\e%[2J%\e%[H%\e%[?25l

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

set /a "PI=31416, HALF_PI=PI / 2, TAU=TWO_PI=2*PI, PI32=PI+HALF_PI, QUARTER_PI=PI / 4"
set "radians=x * pi / 180"
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)%%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!)"
set "cos=(a=(15708 - x)%%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!)"
set "_sin="








set "r0=250"
set "r1=130"
set /a "ax=wid / 2"
set /a "ay=hei / 2"

set /a "step=%radians:x=36%"
set /a "step2=%radians:x=12%"
for /l %%p in (0,%step2%,%tau%) do set /a "ct=!cos:x=%%p!", "st=!sin:x=%%p!" & set "pre_a=!pre_a!"!ct! !st!" "
for /l %%i in (0,%step%,%tau%)  do set /a "ct=!cos:x=%%i!", "st=!sin:x=%%i!" & set "pre_b=!pre_b!"!ct! !st!" "



                               for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t1=((((10%%a-1000)*60+(10%%b-1000))*60+(10%%c-1000))*100)+(10%%d-1000)"
for /l %%# in (1,1,10000) do ( for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=((((10%%a-1000)*60+(10%%b-1000))*60+(10%%c-1000))*100)+(10%%d-1000), dt=t2 - t1"
    
    set /a "radx=dt * 90", "rady=dt * 90", "radz=dt * 80",^
           "crx=!cos:x=radx!", "srx=!sin:x=radx!",^
           "cry=!cos:x=rady!", "sry=!sin:x=rady!",^
           "crz=!cos:x=radz!", "srz=!sin:x=radz!"
    
    set "$donut="
    
	for %%P in (%pre_b%) do for /f "tokens=1,2" %%a in ("%%~P") do (
		set /a "lr= r0 + (r1 * %%a)/10000", "bz=(r1 * %%b)/10000"
	for %%P in (%pre_a%) do for /f "tokens=1,2" %%p in ("%%~P") do (
		set /a "bx=(lr * %%p)/10000",^
			   "by=(lr * %%q)/10000",^
			   "ny=(by * crx - bz * srx)/10000", "nz=( by * srx + bz * crx)/10000",^
			   "nx=(bx * cry + nz * sry)/10000", "nz=(-bx * sry + nz * cry)/10000",^
			   "df=10000 / (1000 - nz)",^
			   "cx=((nx * crz - ny * srz)/10000) * df / 100 + ax",^
			   "cy=((nx * srz + ny * crz)/10000) * df / 100 + ay"
			   
		if !nz! gtr 0 ( set "char=O" ) else ( set "char=." )
		set "$donut=!$donut!%\e%[!cy!;!cx!H!char!"
		
    ))
    
    echo=%\e%[2J%\e%[H!$donut!
    set "$donut="
)

Re: 3D rotating donut

Posted: 17 Mar 2025 02:38
by miskox
I am very impressed! Though I don't understand the code at all (I don't even know how to do it 'by hand').

Saso

Re: 3D rotating donut

Posted: 24 Mar 2025 01:22
by T3RRY
miskox wrote:
17 Mar 2025 02:38
I am very impressed! Though I don't understand the code at all (I don't even know how to do it 'by hand').

Saso
This is the reference Icarus used for the maths If you'd like to learn more.

https://youtu.be/iNA4yH7DAN8

Re: 3D rotating donut

Posted: 25 Mar 2025 00:05
by miskox
Thank you. Very interesting. I also checked other videos he has.

Saso

Re: 3D rotating donut

Posted: 29 Mar 2025 08:28
by nnnmmm
replace 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"

with
set "_SIN=a-a*a/20000*a/30000+a*a/20000*a/30000*a/40000*a/50000-a*a/20000*a/30000*a/40000*a/50000*a/60000*a/70000"
this is the issac newton or the 4th order taylor expansion, when a=1 in radian has about 20% error.. better if made it to the 6th order

set "_SIN=a-a*a/20000*a/30000+a*a/20000*a/30000*a/40000*a/50000-a*a/20000*a/30000*a/40000*a/50000*a/60000*a/70000+a*a/20000*a/30000*a/40000*a/50000*a/60000*a/70000*a/80000*a/90000-a*a/20000*a/30000*a/40000*a/50000*a/60000*a/70000*a/80000*a/90000*a/100000*a/110000"
tokens=1-6.... plus 10%%e)... 10%%f)

"_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"
this seemed to have an algorithm protection
ex) 1+3 =4 or (1/2 + 3/40*20)*200/100=4

Re: 3D rotating donut

Posted: 03 May 2025 08:40
by einstein1969
nnnmmm wrote:
29 Mar 2025 08:28
...
"_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"
this seemed to have an algorithm protection
ex) 1+3 =4 or (1/2 + 3/40*20)*200/100=4
there is no protection, if you calculate the third term for x=7000, with your formula it gives 13, with the other formula it gives 14. The real value is 14.0058...

The first formula tries to reduce errors by exploiting almost the maximum 32bit precision. So there is no need to have more terms and therefore it is more precise and faster.

It could be further optimized, but we must keep in mind that the formula is almost 10 years old. And back then, those who developed it did not have the knowledge they have now.