#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
using namespace std;
struct Point3D{
float x;
float y;
float z;
Point3D()
{
x = 0; y = 0; z = 0;
}
Point3D(float x)
{
this->x = x; y = 0; z = 0;
}
Point3D(float x,float y,float z=0)
{
this->x = x;
this->y = y;
this->z = z;
}
};
enum EleType{TRI,QUD,TET,HEX};
struct Element {
EleType elementType;
Point3D nodes[8];
Element(Point3D p0, Point3D p1, Point3D p2)
{
elementType = TRI;
nodes[0].x = p0.x;
nodes[1].x = p1.x;
nodes[2].x = p2.x;
nodes[0].y = p0.y;
nodes[1].y = p1.y;
nodes[2].y = p2.y;
}
Element(Point3D p0, Point3D p1, Point3D p2, Point3D p3)
{
elementType = QUD;
nodes[0].x = p0.x;
nodes[1].x = p1.x;
nodes[2].x = p2.x;
nodes[3].x = p3.x;
nodes[0].y = p0.y;
nodes[1].y = p1.y;
nodes[2].y = p2.y;
nodes[3].y = p3.y;
}
};
float distance(Point3D a, Point3D b)//两点之间的距离
{
float d = 0;
d = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z);
return sqrt(d);
}
float tri_area(float a, float b, float c) //三角形面积
{
return 1.0 / 4 * sqrt((a + b + c) * (a + b - c) * (a + c - b) * (b + c - a));
}
float area(Element E)
{
float s = 0;
float a, b, c, d,e;
if (E.elementType == TRI)
{
a = distance(E.nodes[0], E.nodes[1]);
b = distance(E.nodes[1], E.nodes[2]);
c = distance(E.nodes[2], E.nodes[0]);
s = tri_area(a,b,c);
}
if (E.elementType == QUD)
{
a = distance(E.nodes[0], E.nodes[1]);
b = distance(E.nodes[1], E.nodes[2]);
c = distance(E.nodes[2], E.nodes[0]);
d = distance(E.nodes[0], E.nodes[3]);
e = distance(E.nodes[2], E.nodes[3]);
s = tri_area(a, b, c)+ tri_area(e, d, c);
}
return s;
}
int main()
{
Point3D points[12] = {
Point3D(20.,15.),
Point3D(-15.,15.),
Point3D(20.,-16.25),
Point3D(-15.,-16.25),
Point3D(-36.2392235,22.0970879),
Point3D(-41.25,-10.),
Point3D(-22.0970879,-36.2392235),
Point3D(10.,-41.25),
Point3D(36.2392235,-22.0970879),
Point3D(41.25,10.),
Point3D(22.0970879,36.2392235),
Point3D(-10.,41.25)
};
for (int i = 0; i < 12; i++)
{
stringstream xx,yy;
xx << points[i].x;
yy << points[i].y;
string x = xx.str();
string y = yy.str();
cout << i+1 << ",\t";
cout << x << ",\t" << y << "\n";
}
Element elements[8] =
{
Element(points[10 - 1],points[11 - 1],points[1 - 1]),
Element(points[8 - 1],points[9 - 1],points[3 - 1]),
Element(points[6 - 1],points[7 - 1],points[4 - 1]),
Element(points[12 - 1],points[5 - 1],points[1 - 1]),
Element(points[12 - 1],points[2 - 1],points[1 - 1],points[11 - 1]),
Element(points[2 - 1],points[5 - 1],points[6 - 1],points[4 - 1]),
Element(points[4 - 1],points[7 - 1],points[8 - 1],points[3 - 1]),
Element(points[3 - 1],points[9 - 1],points[10 - 1],points[1 - 1])
};
cout << "请输入单元编号[1-8]:";
int n;
cin >> n;
if (n < 1 || n>8)//非常输入退出
{
cout << "输入的的单元编号不存在~!" << endl;
}
else
{
cout << "单元编号为" << n << "图形面积是:" << area(elements[n - 1]) << endl;
}
float s = 0;
for (int i = 0; i < 8; i++)
{
s += area(elements[i]);
}
cout << "8个单元编号的所有图形面积是:" << s << endl;
return 0;
}
|