в чем разница между использованием этих двух алгоритмов. Мне всегда было интересно, как я должен что-то оптимизировать, чем они отличаются по памяти и скорости? Один лучше другого? Я имею в виду, помимо ясности кода.
это первая версия, которая у меня была:
bool Intersects(BoundingSphere boundingSphere)
{
D3DXVECTOR3 vectorBetween = (centre - boundingSphere.centre);
// works out the distance between the sphere centre's using pythag
float distance = sqrt(
pow(vectorBetween.x, 2)
+ pow(vectorBetween.y, 2)
+ pow(vectorBetween.z, 2));
// if two radius's add to more than the distance between the centres
return (radius + boundingSphere.radius > distance);
}
Этот метод тот же, но он не хранит никаких значений в переменных, он просто использует одно длинное вычисление
bool Intersects(BoundingSphere boundingSphere)
{
return (radius + boundingSphere.radius >
(sqrt(pow((centre - boundingSphere.centre).x, 2) +
pow((centre - boundingSphere.centre).y, 2) +
pow((centre - boundingSphere.centre).z, 2))));
}