# 怎么求六面体体积

www.MyException.Cn  网友分享于：2013-04-22  浏览：190次

#include "StdAfx.h"

// console.cpp : 定义控制台应用程序的入口点。
//

// 计算四面体体积.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <cmath>

using namespace std;
typedef struct
{
float x,y,z;
}point,vector;

vector ChaCheng(vector v1,vector v2)

{
vector result;
result.x=v1.y*v2.z-v2.y*v1.z;
result.y=v1.x*v2.z-v2.x*v1.z;
result.z=v1.x*v2.y-v2.x*v1.y;
return result;
}

float DianCheng(vector v1,vector v2)
{
float k;
k=v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
return k;
}

bool IsInPlan(point *points)
{
AB.x = (points[1].x-points[0].x) ;
AB.y = (points[1].y-points[0].y) ;
AB.z = (points[1].z-points[0].z) ;

AC.x = (points[2].x-points[0].x) ;
AC.y = (points[2].y-points[0].y) ;
AC.z = (points[2].z-points[0].z) ;

temp1 = ChaCheng(AB,AC);

{
return false;
}
else
{
return true;
}
}

float CalculateVolume(point *points)
{
float STriangle=0,k=0;
vector BC,BD,S,BA;
BC.x=points[2].x-points[1].x;
BC.y=points[2].y-points[1].y;
BC.z=points[2].z-points[1].z;

BD.x=points[3].x-points[1].x;
BD.y=points[3].y-points[1].y;
BD.z=points[3].z-points[1].z;

BA.x=points[0].x-points[1].x;
BA.y=points[0].y-points[1].y;
BA.z=points[0].z-points[1].z;

S=ChaCheng(BC,BD);
k=DianCheng(S,BA);
STriangle =abs(k)/6;

return STriangle;

}
int _tmain(int argc, _TCHAR* argv[])
{
int i=0;
point points[4];
for (i=0;i <4;i++)
{
cout << "Input point["<<i<<"] Information : " <<endl;
cin>>points[i].x>>points[i].y>> points[i].z;
cout <<endl;
}
if (!IsInPlan(points))
{
cout << "The four points are in a plan. " <<endl;
}
cout <<CalculateVolume(points) <<endl;
getchar();
getchar();
return 0;
}

------解决方案--------------------

------解决方案--------------------

|x1 x2 x3 x4|
V(A,B,C,D) = |y1 y2 y3 y4|
|z1 z2 z3 z4|
| 1 1 1 1|

V值若为0，说明4点共面了。