1 #include <iostream> 2 #include <cmath> 3 #include <cstdlib> 4 #include <ctime> 5 using namespace std; 6 7 struct Tpoint{ 8 double x, y, z; 9 }; 10 11 inline double length(Tpoint A, Tpoint B){ 12 double x = A.x - B.x, y = A.y - B.y, z = A.z - B.z; 13 return sqrt(x * x + y * y + z * z); 14 } 15 16 inline double across(Tpoint A, Tpoint B, Tpoint C){ 17 Tpoint p1, p2; 18 p1.x = C.x - A.x; p1.y = C.y - A.y; p1.z = C.z - A.z; 19 p2.x = C.x - B.x; p2.y = C.y - B.y; p2.z = C.z - B.z; 20 double x = p1.y * p2.z - p1.z * p2.y; 21 double y = p1.z * p2.x - p1.x * p2.z; 22 double z = p1.x * p2.y - p1.y * p2.x; 23 //cout<<x<<' '<<y<<' '<<z<<endl; 24 return sqrt(x * x + y * y + z * z); 25 } 26 27 double distance(Tpoint A, Tpoint B, Tpoint C){ 28 return abs(across(A,B,C))/length(B,C); 29 } 30 31 void neijieyuan(Tpoint A, Tpoint B, Tpoint C){ 32 Tpoint cen; 33 34 srand(time(0)); 35 A.x = (double)rand()/rand(); A.y = -(double)rand()/rand(); 36 B.x = -(double)rand()/rand(); B.y = (double)rand()/rand(); 37 C.x = -(double)rand()/rand(); C.y = (double)rand()/rand(); 38 cout<<A.x<<' '<<A.y<<' '<<B.x<<' '<<B.y<<' '<<C.x<<' '<<C.y<<endl; 39 double a = length(B,C), b = length(C, A), c = length(A, B); 40 //cout<<a<<' '<<b<<' '<<c<<endl; 41 if(abs(across(A, B, C)) < 0.01) {cout<<"Wrong Input!"<<endl;return;} 42 cen.x = (a * A.x + b * B.x + c * C.x)/(a + b + c); 43 cen.y = (a * A.y + b * B.y + c * C.y)/(a + b + c); 44 cen.z = (a * A.z + b * B.z + c * C.z)/(a + b + c); 45 cout<<cen.x<<' '<<cen.y<<' '<<cen.z<<endl; 46 cout<<distance(cen, A, B)<<' '<<distance(cen, B, C)<<' '<<distance(cen, C, A)<<endl; 47 } 48 49 int main(){ 50 Tpoint A, B, C; 51 while(true){ 52 //cin>>A.x>>A.y>>A.z>>B.x>>B.y>>B.z>>C.x>>C.y>>C.z; 53 //cout<<across(A, B, C)<<endl; 54 neijieyuan(A, B, C); 55 //cout<<abs(across(A, B, C))/(a + b + c)<<endl; 56 system("pause"); 57 } 58 }
#include <iostream> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; struct Tpoint{ double x, y, z; }; inline double length(Tpoint A, Tpoint B){ double x = A.x - B.x, y = A.y - B.y, z = A.z - B.z; return sqrt(x * x + y * y + z * z); } inline double across(Tpoint A, Tpoint B, Tpoint C){ Tpoint p1, p2; p1.x = C.x - A.x; p1.y = C.y - A.y; p1.z = C.z - A.z; p2.x = C.x - B.x; p2.y = C.y - B.y; p2.z = C.z - B.z; double x = p1.y * p2.z - p1.z * p2.y; double y = p1.z * p2.x - p1.x * p2.z; double z = p1.x * p2.y - p1.y * p2.x; //cout<<x<<' '<<y<<' '<<z<<endl; return sqrt(x * x + y * y + z * z); } double distance(Tpoint A, Tpoint B, Tpoint C){ return abs(across(A,B,C))/length(B,C); } void neijieyuan(Tpoint A, Tpoint B, Tpoint C){ Tpoint cen; srand(time(0)); A.x = (double)rand()/rand(); A.y = -(double)rand()/rand(); B.x = -(double)rand()/rand(); B.y = (double)rand()/rand(); C.x = -(double)rand()/rand(); C.y = (double)rand()/rand(); cout<<A.x<<' '<<A.y<<' '<<B.x<<' '<<B.y<<' '<<C.x<<' '<<C.y<<endl; double a = length(B,C), b = length(C, A), c = length(A, B); //cout<<a<<' '<<b<<' '<<c<<endl; if(abs(across(A, B, C)) < 0.01) {cout<<"Wrong Input!"<<endl;return;} cen.x = (a * A.x + b * B.x + c * C.x)/(a + b + c); cen.y = (a * A.y + b * B.y + c * C.y)/(a + b + c); cen.z = (a * A.z + b * B.z + c * C.z)/(a + b + c); cout<<cen.x<<' '<<cen.y<<' '<<cen.z<<endl; cout<<distance(cen, A, B)<<' '<<distance(cen, B, C)<<' '<<distance(cen, C, A)<<endl; } int main(){ Tpoint A, B, C; while(true){ //cin>>A.x>>A.y>>A.z>>B.x>>B.y>>B.z>>C.x>>C.y>>C.z; //cout<<across(A, B, C)<<endl; neijieyuan(A, B, C); //cout<<abs(across(A, B, C))/(a + b + c)<<endl; system("pause"); } }