标签:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 using namespace std; 6 struct point{ 7 int x,y; 8 point(int x = 0,int y = 0){ 9 this->x = x; 10 this->y = y; 11 } 12 }; 13 struct line{ 14 point st,en; 15 line(){} 16 }; 17 class Intersect { 18 private: 19 line la,lb; 20 public: 21 void readData(); 22 void process(); 23 bool isCross();// 24 bool isSameLine(); 25 }; 26 void Intersect::readData(){ 27 point p1,p2,p3,p4; 28 cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y>>p4.x>>p4.y; 29 la.st = p1;la.en = p2; 30 lb.st = p3;lb.en = p4; 31 } 32 void Intersect::process(){ 33 if(isCross()){ 34 //求出交点 35 double A1 = la.st.y - la.en.y,B1 = la.en.x - la.st.x; 36 double C1 = la.st.x * la.en.y - la.st.y * la.en.x;//A1,B1,C1是方程系数 37 double A2 = lb.st.y - lb.en.y,B2 = lb.en.x - lb.st.x; 38 double C2 = lb.st.x * lb.en.y - lb.st.y * lb.en.x; 39 double x = (B1*C2 - B2*C1)/(A1*B2 - A2*B1); 40 double y = (C1*A2 - C2*A1)/(A1*B2 - A2*B1);//方程的解 41 printf("POINT %.2lf %.2lf\n",x,y); 42 } 43 else{ 44 if(isSameLine()==0){ 45 cout<<"LINE"<<endl; 46 } 47 else 48 cout<<"NONE"<<endl; 49 } 50 } 51 bool Intersect::isCross(){//判断是否是相交的 52 if((la.en.x - la.st.x)*(lb.en.y - lb.st.y) == 53 (la.en.y - la.st.y)*(lb.en.x - lb.st.x)){ 54 return false; 55 } 56 else 57 return true; 58 } 59 bool Intersect::isSameLine(){//用向量叉积判断点是否在线上 60 return (lb.st.x-lb.en.x)*(la.st.y-lb.en.y)-(la.st.x-lb.en.x)*(lb.st.y-lb.en.y); 61 } 62 int main() 63 { 64 int cases; 65 Intersect intersect; 66 #ifndef ONLINE_JUDGE 67 freopen("D://acm.txt","r",stdin); 68 #endif // ONLINE_JUDGE 69 while(cin>>cases){ 70 cout<<"INTERSECTING LINES OUTPUT"<<endl; 71 while(cases--){ 72 intersect.readData(); 73 intersect.process(); 74 } 75 cout<<"END OF OUTPUT"<<endl; 76 } 77 return 0; 78 }
标签:
原文地址:http://www.cnblogs.com/ohxiaobai/p/4524929.html