The beauty of Recursion

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

The beauty of Recursion

#1 Post by IcarusLives » 07 May 2019 21:59

Just wanted to share some simple recursive functions as a friendly reminder that recursion is our friend! No help necessary unless you really just find this neat. Please enjoy :)

Image

Code: Select all

@echo off & setlocal enableDelayedExpansion & call :math

:main
	call :circle 75 50 30
goto :main

rem RECURSIVE FUNCTION
:circle x y d
	setlocal
	set /a "x=%1","y=%2","d=%3"
	if !d! gtr 2 (
		for /l %%a in (0,3,360) do (
			set /a "cx=(d) * !cosd:x=%%a! + (x)", "cy=(d) * !sind:x=%%a! + (y)"
			<nul set /p "=%esc%[!cy!;!cx!HÛ"
		)
		call :circle x+d/2+10 y d/2
		call :circle x-d/2-10 y d/2
		call :circle x y+d/2+10 d/2
	) else goto :eof
goto :eof


:math
	( for /f %%a in ('echo prompt $E^| cmd') do set "esc=%%a" ) & echo !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 "sind=(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 "cosd=(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=150", "wid=150"
	mode %wid%,%hei%
goto :eof
Image

Code: Select all

@echo off & setlocal enableDelayedExpansion

( for /f %%a in ('echo prompt $E^| cmd') do set "esc=%%a" ) & echo !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 "sind=(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 "cosd=(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 "forward=DFX+=(?+1)*^!cosd:x=DFA^!, DFY+=(?+1)*^!sind:x=DFA^!"
set "turnLeft=DFA-=(?)"
set "turnRight=DFA+=(?)"
set "push=sX=DFX, sY=DFY, sA=DFA"
set "pop=DFX=sX, DFY=sY, DFA=sA"
set "draw=?=^!?^!%esc%[^!DFY^!;^!DFX^!HÛ"
set "home=DFX=0, DFY=0, DFA=0"
set "cent=DFX=wid/2, DFY=hei/2"
set "penDown=for /l %%a in (1,1,#) do set /a "^!forward:?=1^!" ^& set "^!draw:?=turtleGraphics^!""

set /a "hei=wid=150"
mode %wid%,%hei%
set /a "DFA=270","DFX=wid/2","DFY=hei"

:main
	call :branch 40
goto :main

:branch len
	setlocal
	set /a "len=%1"
	%penDown:#=!len!%
	if !len! gtr 2 (
		set /a "%push%", "%turnRight:?=45%"
		call :branch len-len/3
		set /a "%pop%", "%push%", "%turnLeft:?=45%"
		call :branch len-len/3
		set /a "%pop%"
		echo !turtleGraphics!
	) else goto :eof
goto :eof

Post Reply