hdu 1086(计算几何入门题——计算线段交点个数)

本文介绍了一个简单的几何问题——计算给定线段的所有交点数量,并提供了一段C++代码实现。该问题允许线段在同一点多次相交并计数。通过定义点和线段结构,使用交叉乘积等数学方法判断线段是否相交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7167    Accepted Submission(s): 3480


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
 

 

Author
lcy

 

 

//////////////////////////////////////////////////////////////////////////////直接上的模板解决的,不知是喜是忧啊

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>

using namespace std;
const int MAX=110;
const double eps = 1e-6;

struct point
{
    double x,y;
};

struct beline
{
    point a,b;
};

point p[MAX];
int n=0;
bool dy(double x,double y)
{
  return x>y+eps;
}
bool xy(double x,double y)
{
    return x<y-eps;
}
bool dyd(double x,double y)
{
    return x > y - eps;
}
bool xyd(double x,double y)
{
    return x<y+eps;
}
bool dd(double x,double y)
{
    return fabs(x-y) < eps;
}
double crossProduct(point a,point b,point c)
{
    return (c.x - a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
bool onSegment(point a,point b,point c)
{
    double maxx=max(a.x,b.x);
    double maxy=max(a.y,b.y);
    double minx=min(a.x,b.x);
    double miny=min(a.y,b.y);
    if(dd(crossProduct(a,b,c),0.0)&&dyd(c.x,minx)&&xyd(c.x,maxx)&&dyd(c.y,miny)&&xyd(c.y,maxy))
        return true;
    return false;
}

bool segIntersect(point p1,point p2,point p3,point p4)
{
    double d1 = crossProduct(p3,p4,p1);
    double d2 = crossProduct(p3,p4,p2);
    double d3 = crossProduct(p1,p2,p3);
    double d4 = crossProduct(p1,p2,p4);
    if(xy(d1 * d2,0.0)&&xy(d3*d4,0.0))
        return true;
    if(dd(d1,0.0)&&onSegment(p3,p4,p1))
        return true;
    if(dd(d2,0.0)&&onSegment(p3,p4,p2))
        return true;
    if(dd(d3,0.0)&&onSegment(p1,p2,p3))
        return true;
    if(dd(d4,0.0)&&onSegment(p1,p2,p4))
        return true;
    return false;
}


int main()
{
    int cas,i,j;
    while(scanf("%d",&cas)!=EOF&&cas!=0)
    {
         beline L[MAX];
         n=0;
         for(i=0;i<cas;i++)
         {
             scanf("%lf%lf%lf%lf",&L[i].a.x,&L[i].a.y,&L[i].b.x,&L[i].b.y);
         }
         for(i=0;i<cas;i++)
         {
             for(j=i+1;j<cas;j++)
                 if(segIntersect(L[i].a,L[i].b,L[j].a,L[j].b))
                 {
                     n++;
                 }
         }
         printf("%d\n",n);
    }
    return 0;
}
View Code

转载于:https://www.cnblogs.com/ccccnzb/p/3861463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值