私のゲームでは、特定の時間に2つのオブジェクト間の距離を見つける必要があります。私の質問は、単にfindDistance関数を改良することができますか?私は何の問題もないようですが、私はそれがもっとうまくいったのか不思議です。また、3D設定でこれを行う場合、別の軸があるので、どのように計算するのですか?2つのオブジェクト間の距離を求める
#include <math.h>
#include <iostream>
struct point{float x, y;};
float findDistance(point &x, point &y)
{
float distance, tempx, tempy;
tempx = (x.x - y.x);
tempx = pow(tempx, 2.0f);
tempy = (x.y - y.y);
tempy = pow(tempy, 2.0f);
distance = tempx + tempy;
distance = sqrt(distance);
return distance;
}
int main()
{
point a, b;
a.x = -2;
a.y = -3;
b.x = -4;
b.y = 4;
std::cout << "The distance between point x and y is: " << findDistance(a, b) << std::endl;
return 0;
}
「float distance」を関数変数としての入力「float distance」とし、スパゲッティコーディングを引き起こすマイクロ最適化を指定します。 –
@SaeedAmiri:私はそれが醜い、混乱していることに同意します。 [Ideone](http://ideone.com/NtMut)とMSVC10では、私の比較は非常に速いと言われています(どれくらい比較することはできませんが、それは私の "空の"ループと同じかそれ以上の速さです)。コーデパッドはATMを破ったようです。 –