# Distance squared

Hello just a quick question I can’t figure out.

Why does Vector3f class have distance squared, why would someone need to use it?

Cause for example a^2 + b^2 = c^2 ?

@normen said:
Cause for example a^2 + b^2 = c^2 ?

Oh I thought that distance squared returns distance^2.
Thanks

Taking the squareroot is an expensive operation, and there are many equations which use squares, i.e an equation of a circle (x-h)^2 + (y-k)^2 = r^2 etc… This is the post I read which actually made me aware of it. The person is trying to find if a point is within a circle.

You can use Pythagoras to measure the distance between your point and the centre and see if it's lower than the radius:

def in_circle(center_x, center_y, radius, x, y):
dist = math.sqrt((center_x - x) ** 2 + (center_y - y) ** 2)

EDIT (hat tip to Paul)

In practice, squaring is often much cheaper than taking the square root and since we're only interested in an ordering, we can of course forego taking the square root:

def in_circle(center_x, center_y, radius, x, y):
square_dist = (center_x - x) ** 2 + (center_y - y) ** 2
return square_dist <= radius ** 2

http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle
@ivandonat said:
Oh I thought that distance squared returns distance^2.

It does... but for comparing distances you can compare 2 squared distances so you don't do 2 expensive sqrt() on them.
@Kova said:
It does... but for comparing distances you can compare 2 squared distances so you don't do 2 expensive sqrt() on them.

But why compare squared distances?
Why not just normal

to get “normal” you need to do a sqrt, which you want to avoid

@wezrule said:
to get "normal" you need to do a sqrt, which you want to avoid

No I mean,
you have distance(Vectorf3f vec) and distancesq(Vectorf3f vec),
Why would you need distancesq when you can use distance?

oO you wouldn’t, only when you can avoid further computations along the way, thats what everybody tries to tell you here. For programming games you need to do a lot of math, if some of that is already computed and you don’t have to that saves CPU cycles.

@normen said:
oO you wouldn't, only when you can avoid further computations along the way, thats what everybody tries to tell you here. For programming games you need to do a lot of math, if some of that is already computed and you don't have to that saves CPU cycles.

Okay so all I have to do now for simple maths is to use Vector3f.distancesq instead of Vector3f.distance?

:facepalm: no. don’t replace all distance with distancesq, only use it when you know you want it. as thats clearly not the case, just leave it alone.

@normen said:
:facepalm: no. don't replace all distance with distancesq, only use it when you know you want it. as thats clearly not the case, just leave it alone.

Okay thanks :3

if you need distance as value for further calculations, just use distance()

if you need distance to compare to other distance, use distanceSquared()

ok let’s go from start…

lets say you have 3 points, A(xa, ya, za), B(xb, yb, zb), C(xc, yc, zc). If you need to know if B or C is closer to A, you need to calculate distances using Pythagoras theorem:

(distance A - B) d1 = sqrt ( (xa - xb)(xa - xb) + (ya - yb)(ya - yb) + (za - zb)(za - zb) )

(distance A - C) d2 = sqrt ( (xa - xc)
(xa - xc) + (ya - yc)(ya - yc) + (za - zc)(za - zc) )

but if you need to compare just 2 numbers, you can SKIP the costly SQRT … d1 > d2 provides same result as d1d1 > d2d2 so it doesn’t matter if it’s squared or not.

3 Likes
@Kova said:
if you need distance as value for further calculations, just use distance()
if you need distance to compare to other distance, use distanceSquared()

ok let's go from start...
lets say you have 3 points, A(xa, ya, za), B(xb, yb, zb), C(xc, yc, zc). If you need to know if B or C is closer to A, you need to calculate distances using Pythagoras theorem:
(distance A - B) d1 = sqrt ( (xa - xb)*(xa - xb) + (ya - yb)*(ya - yb) + (za - zb)*(za - zb) )
(distance A - C) d2 = sqrt ( (xa - xc)*(xa - xc) + (ya - yc)*(ya - yc) + (za - zc)*(za - zc) )

but if you need to compare just 2 numbers, you can SKIP the costly SQRT .. d1 > d2 provides same result as d1*d1 > d2*d2 so it doesn't matter if it's squared or not.

Oooooh, that helped a lot :)
Thanks!

To further explain - taking a square root is a pretty complex algorithm that takes time to complete (it isn’t even constant time, some numbers are much easier to square root than others). This means that it takes approximately ten times as long to go from aa->a as it does from a->aa. When you calculate the length (distance) of a vector the algorithm actually gives you the squared value. The final step is then to square root that to get the resulting length. … so it isn’t that you calculate squared from non-squared, it’s the other way around.

So if you are doing this a lot (which can easily happen since as already described squares happen a lot in 3d math) then where possible if you use the a*a values - even if it means squaring b to compare - then it’s a lot faster.