您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: