UVa 378 - Intersecting Lines

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值