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!

P#145602 2024-04-03 19:24 ( Edited 2024-04-05 02:55)

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.

P#145611 2024-04-03 20:32
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
P#145616 2024-04-03 21:51 ( Edited 2024-04-03 22:10)
1

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

P#145765 2024-04-05 02:56

[Please log in to post a comment]