hdu 1086 You can Solve a Geometry Problem too

原创 2012年03月21日 22:18:22

判断线段是否相交的模板题:

#include <iostream>

using namespace std;

struct piont
{
    double x,y;
};
struct segm
{
    piont p1,p2;
}s[110];



double dir(piont pi,piont pj,piont pk)
{
    return (pi.x - pk.x)*(pj.y - pk.y) - (pj.x - pk.x)*(pi.y - pk.y);
}

bool onseg(piont pi,piont pj,piont pk)
{
    if((pk.x >= min(pi.x,pj.x)&&pk.x <= max(pi.x,pj.x))&&(pk.y >= min(pi.y,pj.y)&&pk.y <= max(pi.y,pj.y)))
    return 1;
    else return 0;
}
bool seg(piont p1,piont p2,piont p3,piont p4)
{
    double d1,d2,d3,d4;
    d1 = dir(p3,p4,p1);
    d2 = dir(p3,p4,p2);
    d3 = dir(p1,p2,p3);
    d4 = dir(p1,p2,p4);
    if(((d1 > 0&&d2 < 0)||(d1 < 0&&d2 > 0))&&((d3 > 0&&d4 < 0)||(d3 < 0&&d4 > 0))) return 1;
    else if(d1 == 0&&onseg(p3,p4,p1)) return 1;
    else if(d2 == 0&&onseg(p3,p4,p2)) return 1;
    else if(d3 == 0&&onseg(p1,p2,p3)) return 1;
    else if(d2 == 0&&onseg(p1,p2,p4)) return 1;
    else return 0;
}

int main()
{

    int n,i,j,ans;
    while(cin>>n&&n)
    {
        ans = 0;
        for(i = 0;i < n;i ++)
        {
            cin>>s[i].p1.x>>s[i].p1.y>>s[i].p2.x>>s[i].p2.y;
        }
        for(i = 0;i < n;i ++)
        {
            for(j = i+1;j < n;j ++)
            {
                if(seg(s[i].p1,s[i].p2,s[j].p1,s[j].p2)) ans ++;
            }
        }
        cout<<ans <<endl;
    }
    return 0;
}


相关文章推荐

HDU-1086 You can Solve a Geometry Problem too 解题报告

Description Many geometry(几何)problems were designed in the ACM/ICPC. Andnow, I also prepare a geome...

hdu 1086 You can Solve a Geometry Problem too

题目: B - You can Solve a Geometry Problem too Time Limit:1000MS     Memory Limit:32768KB     64bit ...

线段是否相交问题 HDU 1086 You can Solve a Geometry Problem too

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3...

HDU 1086 You can Solve a Geometry Problem too && 简单几何

题意:给你一百条线段,问这些线段交点的个数,重复交点需要重复计算。 解法:先存下所有线段,然后双重for循环,两两判断是否相交,先求两条直线是否相交,再判断交点是否在线段上,统计个数。 代码: #i...

HDU 1086 You can Solve a Geometry Problem too(判断线段是否相交,非规范相交)

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/...

HDU 1086 You can Solve a Geometry Problem too(判断两线段是否相交)跨立实验

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/...
  • YJX_xx
  • YJX_xx
  • 2014年07月13日 10:31
  • 609

ACM-计算几何之You can Solve a Geometry Problem too——hdu1086

ACM 计算几何 You can Solve a Geometry Problem too hdu1086 线段相交

hdu 1086 You can Solve a Geometry Problem too 向量积

#include #include #include #include #include using namespace std; const double eps=1e-14; const...

HDU1086 You can Solve a Geometry Problem too

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3...

hdu-1086 You can Solve a Geometry Problem too(线段是否相交)

题目链接:点击打开链接 You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory ...
  • acm_cxq
  • acm_cxq
  • 2016年04月25日 22:46
  • 187
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1086 You can Solve a Geometry Problem too
举报原因:
原因补充:

(最多只允许输入30个字)