也就是已知空间不共圆的四点坐标,怎么求这个四点所组成的外接球的半径???
太深奥了
这个问题,我解答出来了,但是没有很多的测试数据,所以不知道对不对……
//main.cpp
#include "classes.h"
void main()
{
Point a;
Point b;
Point c;
Point d;
Equation m (a,b);
Equation n (a,c);
Equation l (a,d);
answer (m,l,n);
}
//classes.cpp
#include "classes.h"
#include <iostream.h>
Point::Point ()
{
cout <<"Please input a point:"; //输入一个点的坐标
cin >>x>>y>>z;
}
float Point::getX ()
{
return x;
}
float Point::getY ()
{
return y;
}
float Point::getZ ()
{
return z;
}
Equation::Equation (Point a,Point b)
{
float i,j,k;
i=(a.getX()+b.getX())/2; //中点
j=(a.getY()+b.getY())/2;
k=(a.getZ()+b.getZ())/2;
x=(a.getX()-b.getX()); //向量
y=(a.getY()-b.getY());
z=(a.getZ()-b.getZ());
t=i*x+j*y+k*z; //方程 各未知数系数即x,y,z t为方程右边常数
}
void answer (Equation a,Equation b,Equation c)
{
int i,j;
float x,y,z,D=0,E=0;
float p[3][4]={{a.x,a.y,a.z,a.t},{b.x,b.y,b.z,b.t},{c.x,c.y,c.z,c.t}};
for (i=0;i<=2;i++)
D+=p[0][i]*p[1][(i+1)%3]*p[2][(i+2)%3];
for (i=0;i<=2;i++)
D-=p[2][i]*p[1][(i+1)%3]*p[0][(i+2)%3];
for (i=0;i<=2;i++) //x
{
j=p[i][0];
p[i][0]=p[i][3];
p[i][3]=j;
}
for (i=0;i<=2;i++)
E+=p[0][i]*p[1][(i+1)%3]*p[2][(i+2)%3];
for (i=0;i<=2;i++)
E-=p[2][i]*p[1][(i+1)%3]*p[0][(i+2)%3];
x=E/D;
for (i=0;i<=2;i++)
{
j=p[i][0];
p[i][0]=p[i][3];
p[i][3]=j;
}
E=0; //y
for (i=0;i<=2;i++)
{
j=p[i][1];
p[i][1]=p[i][3];
p[i][3]=j;
}
for (i=0;i<=2;i++)
E+=p[0][i]*p[1][(i+1)%3]*p[2][(i+2)%3];
for (i=0;i<=2;i++)
E-=p[2][i]*p[1][(i+1)%3]*p[0][(i+2)%3];
y=E/D;
for (i=0;i<=2;i++)
{
j=p[i][1];
p[i][1]=p[i][3];
p[i][3]=j;
}
E=0; //z
for (i=0;i<=2;i++)
{
j=p[i][2];
p[i][2]=p[i][3];
p[i][3]=j;
}
for (i=0;i<=2;i++)
E+=p[0][i]*p[1][(i+1)%3]*p[2][(i+2)%3];
for (i=0;i<=2;i++)
E-=p[2][i]*p[1][(i+1)%3]*p[0][(i+2)%3];
z=E/D;
for (i=0;i<=2;i++)
{
j=p[i][2];
p[i][2]=p[i][3];
p[i][3]=j;
}
cout <<"The point is "<<x<<","<<y<<","<<z<<"\n";
}
//classes.h
class Point
{
public:
Point ();
float getX();
float getY();
float getZ();
private:
float x;
float y;
float z;
};
class Equation
{
public:
Equation (Point a,Point b);
friend void answer (Equation a,Equation b,Equation c);
private:
float x;
float y;
float z;
float t;
};
还请高手指正。