%getDistance% Macro

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

%getDistance% Macro

#1 Post by IcarusLives » 05 Aug 2017 12:38

Hello, I just wanted to share something I made for my own use, but perhaps other will find it useful as well?

The algorithm can be found here https://en.wikipedia.org/wiki/Chebyshev_distance

The %getDistance% macro takes 5 arguments.

%getDistance% x2 x1 y2 y1 VAR

Code: Select all

@echo off & setlocal enableDelayedExpansion

set ^"LF=^

^" Above empty line is required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"

set getDistance=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-5" %%1 in ("^!args^!") do (%\n%
   set /a "%%5=( ?=((((((%%1 - %%2))>>31|1)*((%%1 - %%2)))-((((%%3 - %%4))>>31|1)*((%%3 - %%4))))>>31)+1, ?*(2*((((%%1 - %%2))>>31|1)*((%%1 - %%2)))-((((%%3 - %%4))>>31|1)*((%%3 - %%4)))-(((((%%1 - %%2))>>31|1)*((%%1 - %%2)))-((((%%3 - %%4))>>31|1)*((%%3 - %%4))))) + ^^^!?*(((((%%1 - %%2))>>31|1)*((%%1 - %%2)))-((((%%3 - %%4))>>31|1)*((%%3 - %%4)))-(((((%%1 - %%2))>>31|1)*((%%1 - %%2)))-((((%%3 - %%4))>>31|1)*((%%3 - %%4)))*2)) )"%\n%
)) else set args=

set /a "x1=4", "x2=7", "y1=22", "y2=14"

%getDistance% x2 x1 y2 y1 distance

echo %distance%

pause & exit

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: %getDistance% Macro

#2 Post by Aacini » 05 Aug 2017 21:42

This is the "function" version of the same formula:

Code: Select all

@echo off & setlocal EnableDelayedExpansion

rem Define a "function"
set "getDistance(x2,x1,y2,y1)=( @=x2-x1, $=y2-y1, ?=(((@>>31|1)*@-(($>>31|1)*$))>>31)+1, ?*(2*(@>>31|1)*@-($>>31|1)*$-((@>>31|1)*@-($>>31|1)*$)) + ^^^!?*((@>>31|1)*@-($>>31|1)*$-((@>>31|1)*@-($>>31|1)*$*2)) )"

rem Use the "function"
set /A "x1=4, x2=7, y1=22, y2=14, distance=%getDistance(x2,x1,y2,y1)%"

echo Distance = %distance%

Antonio

Post Reply