Log In  


Resolved

Does anyone know of a long distance function that does not: 1. Overflow, 2. Gives pixel perfect distance, 3. Handles distances up to about 200. I have one I'm working with:

function dist(x,y,x2,y2)
 local opp=x-x2
 local adj=y-y2

 return opp/cos(-atan2(opp,adj))
end

However, it has weird instances where it throws up its hands and gives crazy numbers.

Thanks so much for the help!



1

This is a good one: https://www.lexaloffle.com/bbs/?tid=49827

Last comment in the thread has a version for 3D as well.


2

Here's mine, not the fastest or smallest, but pretty accurate as all 32 bits of numbers are used for precision of the squares.

function dist(x,y,x2,y2)
 local opp=(x-x2)>>8
 local adj=(y-y2)>>8
 return sqrt(opp*opp+adj*adj)<<8
end

> ? dist(-3000,0,0,4000)
5000
> ? dist(0,0,1,1)
1.4141
> ? sqrt(2)
1.4142

1

Thank you both! I grabbed @TetraPengwin's because it was faster.



[Please log in to post a comment]