Tuesday, October 24, 2017

mathematics - Are there any disadvantages of using Distance Squared checks rather than Distance?


I use distance squared checks for basically all my distance (vector3 length) checking, due to the performance increase from not incurring a square root (like in plain length checks).


From the looks of it, squared distance checks work fine in every situation:


if x^2 < y^2, then x < y, even when 0 < (x or y) < 1



I am not considering situations where x or y is less than 0, as distance and distance-squared is always going to be positive.


Since this works, it looks like distance checks are never ever needed, but I have a nagging feeling that i'm missing something. Will this still hold up in accuracy-critical situations?



Answer



There's no disadvantage I'm aware of when using squared length to compare distances. Think about it like that: You're just skipping the sqrt which doesn't give you any additional accuracy. If you don't need the actual Euclidean distance, then you can safely leave the sqrt out.


Of course the squared length scales quite differently than the Euclidean distance and is therefore a bad candidate for things like pathfinding heuristics.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...