I
I
i__egor2021-04-24 14:30:50
Mathematics
i__egor, 2021-04-24 14:30:50

How to find the distance between two points faster?

Who knows a more productive way to find the distance in two-dimensional space from point A to B. You need something faster than exact: sqrt((x1-x2)^2 - (y1-y2)^2) and more accurate than the simplest: modulo (x1-x2)+module(y1-y2)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
longclaps, 2021-04-24
@i__egor

For example like this:

x, y = 1.2345, 3.2109
le0 = sqrt(x * x + y * y)
if x < y:
    x, y = y, x
y /= x
le1 = x * (1.0 + y * y * 0.5)
print(le1 / le0) #  1.0023767214085302, неплохо

In the worst case, when x == y, the error is +6%.
Just not that fast.
There is a faster / easier way, here the error is up to + 8%
if x < y:
    x, y = y, x
le1 = x + y * 0.41421356237309515 # sqrt(2.0) - 1.0

R
Rsa97, 2021-04-24
@Rsa97

What is the end goal of the metric? For example, to sort or check for being within a given radius, you can remove the root.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question