HDU1086判断线段相交

 

HDU1086

You can Solve a Geometry Problem too

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


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
 
  可以把线段看成直线,计算出交点,看交点是不是在可行的范围内;注意一下斜率等于0的情况就行了;
 
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<iomanip>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<vector>
 7 #include<cstdio>
 8 #include<stack>
 9 #include<queue>
10 #include<cmath>
11 #include<ctime>
12 #include<set>
13 #include<map>
14 #define ll long long
15 #define rep(i,a,b) for(register int i=a;i<=b;i++)
16 #define inf 1<<29
17 #define re register
18 using namespace std;
19 #define eps 1e-6
20 const int N=110;
21 struct point{
22     double x,y;
23 }d[2];
24 struct line{
25     double k,b;
26     double l,r,ly,ry;
27 }L[N];
28 int n;
29 inline bool pd(double x,double y,int k) {
30     return (x>=L[k].l&&x<=L[k].r&&y>=L[k].ly&&y<=L[k].ry);
31 }
32 inline bool judge(int i,int j) {
33     if(fabs(L[i].k-L[j].k)<=eps) return 0;
34     if(fabs(L[i].k-0)<=eps&&fabs(L[j].k-0)<=eps) return 0;
35     if(fabs(L[i].k-0)>eps&&fabs(L[j].k-0)>eps) {
36         double x=(L[j].b-L[i].b)/(L[i].k-L[j].k),
37             y=L[i].k*x+L[i].b;
38         if(pd(x,y,i)&&pd(x,y,j)) return 1;
39         else return 0;
40     }
41     if(fabs(L[i].k-0)<=eps) {
42         double x=L[i].l;
43         double y=L[j].k*x+L[j].b;
44         return (y>=L[j].ly&&y<=L[j].ry);
45     }
46     double x=L[j].l;
47     double y=L[i].k*x+L[i].b;
48     return (y>=L[i].ly&&y<=L[i].ry);
49 }
50 int main() {
51     freopen("Y.in","r",stdin);
52     freopen("Y.out","w",stdout);
53     while(scanf("%d",&n)&&n) {
54         for(int i=1;i<=n;i++) {
55             scanf("%lf%lf%lf%lf",&d[0].x,&d[0].y,&d[1].x,&d[1].y);
56             L[i].l=min(d[0].x,d[1].x),L[i].r=max(d[0].x,d[1].x);
57             L[i].ly=min(d[0].y,d[1].y),L[i].ry=max(d[0].y,d[1].y);
58             if(fabs(d[1].x-d[0].x)<=eps)
59                 L[i].k=0;
60             else {
61                 L[i].k=(d[1].y-d[0].y)/(d[1].x-d[0].x);
62                 L[i].b=d[0].y-L[i].k*d[0].x;
63             }
64         }
65         int ans=0;
66         for(int i=1;i<n;i++)
67             for(int j=i+1;j<=n;j++)
68                 if(judge(i,j)) ans++;
69         cout<<ans<<endl;
70     }
71     return 0;
72 }

 

转载于:https://www.cnblogs.com/ypz999/p/7107127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值