HDU 1086 You can Solve a Geometry Problem too(判断两条直线是否相交)

来源:互联网 发布:视频剪辑软件免费下载 编辑:程序博客网 时间:2024/06/06 18:00

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1086

题解:

单纯的判断

AC代码:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;struct line{    double x1,x2,y1,y2;}ln[1000];int solve(int i, int j){    int flag = 0;    double dac=(ln[i].x1-ln[j].x1)*(ln[i].y1-ln[i].y2)-(ln[i].x1-ln[i].x2)*(ln[i].y1-ln[j].y1);    double dbc=(ln[i].x1-ln[j].x2)*(ln[i].y1-ln[i].y2)-(ln[i].x1-ln[i].x2)*(ln[i].y1-ln[j].y2);    double acb=(ln[j].x1-ln[i].x1)*(ln[j].y1-ln[j].y2)-(ln[j].x1-ln[j].x2)*(ln[j].y1-ln[i].y1);    double adb=(ln[j].x1-ln[i].x2)*(ln[j].y1-ln[j].y2)-(ln[j].x1-ln[j].x2)*(ln[j].y1-ln[i].y2);    if(acb * adb <= 0)        flag ++;    if(dbc * dac <= 0)        flag ++;    if(flag == 2)        return 1;    else        return 0;}int main(){    int t,ans;    while(cin >> t)    {        if(t == 0)break;        ans = 0;        for(int i = 1; i <= t; i++)        {            cin >> ln[i].x1 >> ln[i].y1 >> ln[i].x2 >> ln[i].y2;        }        for(int i = 1; i < t; i++)        {            for(int j = i+1; j <= t; j++)            {                if(solve(i,j) == 1)ans++;            }        }        cout << ans <<endl;    }    return 0;}
0 0
原创粉丝点击