三次元平面上に置かれた二点 \(P_{1}(x_{1},y_{1},z_{1}),P_{2}(x_{2},y_{2},z_{2})\) 間の距離\(d(P_{1},P_{2})\)は
\begin{align}
d(P_{1},P_{2})=\sqrt{(x_{2}-x_{1})^2+(y_{2}-y_{1})^2+(z_{2}-z_{1})^2}
\end{align}
で与えられる。
実行結果
3.4641
ソースコード
#include <iostream>
typedef struct {
double x;
double y;
double z;
}Point;
double Distance(Point, Point);
int main() {
Point P1, P2;
P1.x = 1;
P1.y = 2;
P1.z = 3;
P2.x = 3;
P2.y = 4;
P2.z = 1;
std::cout << Distance(P1, P2) << std::endl;
}
double Distance(Point P1, Point P2) {
double d;
double x, y, z;
x = P2.x - P1.x;
y = P2.y - P1.y;
z = P2.z - P1.z;
d = std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2));
return d;
}
コメント