您的位置:首页 > 其它

hdu 1086

2015-06-08 22:32 417 查看


You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8586 Accepted Submission(s): 4177



Problem Description

Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now
attending an exam, not a contest :)

Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.

Note:

You can assume that two segments would not intersect at more than one point.



Input

Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.

A test case starting with 0 terminates the input and this test case is not to be processed.



Output

For each case, print the number of intersections, and one line one case.



Sample Input

2
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
3
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.000
0.00 0.00 1.00 0.00
0




Sample Output

1
3


求多条线段共有几个交点 不计算重复的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<string>
#include<map>
#define ll __int64
#define minn 1e-8
using namespace std;
struct Point
{
    double x,y;
}P[10010];
struct Line
{
    Point a,b;
}L[110];
///------------alg 2------------
//叉积
double mult(Point a, Point b, Point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}

//aa, bb为一条线段两端点 cc, dd为另一条线段的两端点 相交返回true, 不相交返回false
bool intersect(Point aa, Point bb, Point cc, Point dd)
{
    if ( max(aa.x, bb.x)<min(cc.x, dd.x) )
    {
        return false;
    }
    if ( max(aa.y, bb.y)<min(cc.y, dd.y) )
    {
        return false;
    }
    if ( max(cc.x, dd.x)<min(aa.x, bb.x) )
    {
        return false;
    }
    if ( max(cc.y, dd.y)<min(aa.y, bb.y) )
    {
        return false;
    }
    if ( mult(cc, bb, aa)*mult(bb, dd, aa)<0 )
    {
        return false;
    }
    if ( mult(aa, dd, cc)*mult(dd, bb, cc)<0 )
    {
        return false;
    }
    return true;
}
///------------alg 2------------
double cross(double x1, double y1, double x2, double y2)
{
    return x1 * y2 - x2 * y1;
}
void intersec(Point a, Point b, Point c, Point d, double &x, double &y)
{
    double u = cross(d.x - a.x, d.y - a.y, b.x - a.x, b.y - a.y);
    double v = cross(c.x - a.x, c.y - a.y, b.x - a.x, b.y - a.y);
    double w = u - v;
    x = (u * 1.0 * c.x - v * 1.0 * d.x) / (w * 1.0);
    y = (u * 1.0 * c.y - v * 1.0 * d.y) / (w * 1.0);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        int len=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&L[i].a.x,&L[i].a.y,&L[i].b.x,&L[i].b.y);
            for(int j=0;j<i;j++)
            {
                if(intersect(L[i].a,L[i].b,L[j].a,L[j].b))
                {
                    double st,ed;
                    bool flag=true ;
                    intersec(L[i].a,L[i].b,L[j].a,L[j].b,st,ed);
                    for(int k=0;k<len;k++)
                    {
                        if(abs(P[k].x-st)<minn&&abs(P[k].y-ed)<minn)
                        {
                            flag=false ;
                            break;
                        }
                    }
                    if(flag)
                        P[len].x=st,P[len].y=ed,len++;
                }
            }
        }
        printf("%d\n",len);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: