UVa 378 - Intersecting Lines
2016-04-05 23:00
253 查看
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; struct point{ int x,y; point(int x = 0,int y = 0){ this->x = x; this->y = y; } }; struct line{ point st,en; line(){} }; class Intersect { private: line la,lb; public: void readData(); void process(); bool isCross();// bool isSameLine(); }; void Intersect::readData(){ point p1,p2,p3,p4; cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y>>p4.x>>p4.y; la.st = p1;la.en = p2; lb.st = p3;lb.en = p4; } void Intersect::process(){ if(isCross()){ //求出交点 double A1 = la.st.y - la.en.y,B1 = la.en.x - la.st.x; double C1 = la.st.x * la.en.y - la.st.y * la.en.x;//A1,B1,C1是方程系数 double A2 = lb.st.y - lb.en.y,B2 = lb.en.x - lb.st.x; double C2 = lb.st.x * lb.en.y - lb.st.y * lb.en.x; double x = (B1*C2 - B2*C1)/(A1*B2 - A2*B1); double y = (C1*A2 - C2*A1)/(A1*B2 - A2*B1);//方程的解 printf("POINT %.2lf %.2lf\n",x,y); } else{ if(isSameLine()==0){ cout<<"LINE"<<endl; } else cout<<"NONE"<<endl; } } bool Intersect::isCross(){ //判断是否是相交的 if((la.en.x - la.st.x)*(lb.en.y - lb.st.y) == (la.en.y - la.st.y)*(lb.en.x - lb.st.x)){ return false; } else return true; } bool Intersect::isSameLine(){ //用向量叉积判断点是否在线上 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); } int main() { int cases; Intersect intersect; #ifndef ONLINE_JUDGE freopen("D://acm.txt","r",stdin); #endif // ONLINE_JUDGE while(cin>>cases){ cout<<"INTERSECTING LINES OUTPUT"<<endl; while(cases--){ intersect.readData(); intersect.process(); } cout<<"END OF OUTPUT"<<endl; } return 0; }
相关文章推荐
- solr学习基础
- mono+jexus 验证码不显示:System.Drawing
- 结对编程 黄金点游戏
- STL学习系列四:Stack容器
- socket
- JavaScript编码解码
- Nginx On rhel6.4
- NSCopying、NSCoding
- 有序链表合并
- Android开发实践:使用Service还是Thread
- 文件的结构
- oprofile
- pi
- 推荐 10 款最好的 Python IDE
- 我是一个线程(对理解多线程很有帮助)
- 推荐 10 款最好的 Python IDE
- 修改maven本地仓库存放位置
- Android React-Native系列之<一>零基础搭建React-Native开发环境
- PHP中array_multisort与asort,arsort的用法
- 【codechef】交换字符串S的两个位置上的字符,求有多少对AB不相似