Каковы различия между использованием этих двух алгоритмов.Я всегда задавался вопросом, как я должен оптимизировать вещи. Как они отличаются памятью и скоростью?Один лучше другого?Помимо ясности кода я имею в виду.
это моя первая версия:
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))));
}