LERP() function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

LERP() function

#1 Post by IcarusLives » 30 Apr 2019 22:59

Hello,

I finally figure out my LERP function formula! Please view https://en.wikipedia.org/wiki/Linear_interpolation

Code: Select all

set "lerp=?=(a+c*(b-a)*1000)/100000+a"
Tested with this code, trying to simulate the "chaos game"

Code: Select all

@echo off & setlocal enableDelayedExpansion & mode 100,100
( for /f %%a in ('echo prompt $E^| cmd') do set "esc=%%a" ) & echo !esc![?25l

set "lerp=?=(a+c*(b-a)*1000)/100000+a"
set /a "hei=80", "wid=80"
set /a "ax=wid/2","ay=0"
set /a "bx=0","by=hei"
set /a "cx=wid","cy=hei"
set /a "x=!random! %% wid + 1"
set /a "y=!random! %% hei + 1"

for /l %%# in () do (

		set /a "r=!random! %% 3"
		if !r! equ 0 (
			<nul set /p "=%esc%[38;5;9m"
			set /a "a=x", "b=ax", "c=50", "%lerp:?=x%"
			set /a "a=y", "b=ay", "c=50", "%lerp:?=y%"
		) else if !r! equ 1 (
			<nul set /p "=%esc%[38;5;10m"
			set /a "a=x", "b=bx", "c=50", "%lerp:?=x%"
			set /a "a=y", "b=by", "c=50", "%lerp:?=y%"
		) else if !r! equ 2 (
			<nul set /p "=%esc%[38;5;11m"
			set /a "a=x", "b=cx", "c=50", "%lerp:?=x%"
			set /a "a=y", "b=cy", "c=50", "%lerp:?=y%"
		)
		<nul set /p "=%esc%[!y!;!x!HÛ"

)
Image

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

Re: LERP() function

#2 Post by IcarusLives » 01 May 2019 23:35

Solved my original problem, and released the function. Hope it comes useful!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: LERP() function

#3 Post by dbenham » 02 May 2019 05:45

I don't understand what is going on, but I can simplify your code a bit and improve performance :)

Code: Select all

@echo off & setlocal enableDelayedExpansion & mode 100,100
( for /f %%a in ('echo prompt $E^| cmd') do set "esc=%%a" ) & echo !esc![?25l

set "lerp=?=(a+c*(b-a)*1000)/100000+a"
set /a "hei=80, wid=80"
set /a "x0=wid/2, y0=0"
set /a "x1=0,     y1=hei"
set /a "x2=wid,   y2=hei"
set /a "x=!random! %% wid + 1"
set /a "y=!random! %% hei + 1"
set /a "h0=9, h1=10, h2=11"

for /l %%# in () do (
  set /a "r=!random! %% 3"
  set /a "h=h!r!"
  set /a "a=x", "b=x!r!", "c=50", "%lerp:?=x%"
  set /a "a=y", "b=y!r!", "c=50", "%lerp:?=y%"
  <nul set /p "=%esc%[38;5;!h!m%esc%[!y!;!x!HÛ"
)
I think a tiny bit more performance can be squeezed out by combining the last three SET /A statements into one, but I don't think it is worth it.

Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: LERP() function

#4 Post by dbenham » 02 May 2019 06:09

Actually, by escaping the closing parens in the LERP definition, you can gain that last bit of performance and preserve the formatting by combining the last three statements with line continuation.

Code: Select all

@echo off & setlocal enableDelayedExpansion & mode 100,100
( for /f %%a in ('echo prompt $E^| cmd') do set "esc=%%a" ) & echo !esc![?25l

set "lerp=?=(a+c*(b-a^)*1000^)/100000+a"
set /a "hei=80, wid=80"
set /a "x0=wid/2, y0=0"
set /a "x1=0,     y1=hei"
set /a "x2=wid,   y2=hei"
set /a "x=!random! %% wid + 1"
set /a "y=!random! %% hei + 1"
set /a "h0=9, h1=10, h2=11"

for /l %%# in (1 1 10000) do (
  set /a r=!random! %% 3
  set /a h=h!r!,^
         a=x, b=x!r!, c=50, %lerp:?=x%,^
         a=y, b=y!r!, c=50, %lerp:?=y%
  <nul set /p "=%esc%[38;5;!h!m%esc%[!y!;!x!HÛ"
)
Dave Benham

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

Re: LERP() function

#5 Post by IcarusLives » 02 May 2019 09:42

Hey! Thanks for the performance update!

More or less, I just wanted to demonstrate the function with some very readable code. My hopes were that it would appear "easy to use".

LERP is useful for a lot things, but I am still learning about it.

The code below demonstrates how LERP can be used to "follow a target". Also, a bonus function I wrote called EVERY. Outputs 1 if true 0 if false, invokes divide by 0. In this case we can say something like "every N %frames%"

Code: Select all

@echo off & setlocal enableDelayedExpansion & mode 100,100
( for /f %%a in ('echo prompt $E^| cmd') do set "esc=%%a" ) & echo !esc![?25l

set "lerp=?=(a+c*(b-a^)*1000^)/100000+a"
set "every=1/(((~(0-(frames%%n))>>31)&1)&((~((frames%%n)-0)>>31)&1))"

set /a "hei=wid=100"

set /a "x1=70", "y1=80"
set /a "x2=5",  "y2=5"


for /l %%# in () do (

	set /a "frames+=1"

	2>nul set /a "%every:n=25%" && (
	set /a a=x1, b=x2, c=20, x1=%lerp%,^
		   a=y1, b=y2, c=20, y1=%lerp%
	)
	<nul set /p "=%esc%[2J%esc%[38;5;10m%esc%[!y1!;!x1!HX%esc%[38;5;9m%esc%[!y2!;!x2!HO"
)


pause>nul


Post Reply