前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ1086You can Solve a Geometry Problem too

HDUOJ1086You can Solve a Geometry Problem too

作者头像
Gxjun
发布2018-03-22 10:42:22
4640
发布2018-03-22 10:42:22
举报
文章被收录于专栏:ml

You can Solve a Geometry Problem too

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

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

 线段是否相交的判断,采用的石墨板,..

代码:

代码语言:javascript
复制
 1 #include<stdio.h>
 2 #include<math.h>
 3 const double eps=1e-10 ;
 4 typedef struct
 5 {
 6     double x,y;
 7 }point;
 8 
 9 double min(double a, double b)
10 {
11     return a<b?a:b;
12 }
13 double max(double a,double b)
14 {
15     return a>b?a:b;
16 }
17 //判断线段是否有焦点
18 bool inter(point a ,point b, point c ,point d)
19 {
20     if(min(a.x,b.x)>max(c.x,d.x)||min(a.y,b.y)>max(c.y,d.y)||
21       min(c.x,d.x)>max(a.x,b.x)||min(c.y,d.y)>max(a.y,b.y))
22       return 0;
23     double h,i,j,k;
24     h=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
25     i=(b.x-a.x)*(d.y-a.y)-(b.y-a.y)*(d.x-a.x);
26     j=(d.x-c.x)*(a.y-c.y)-(d.y-c.y)*(a.x-c.x);
27     k=(d.x-c.x)*(b.y-c.y)-(d.y-c.y)*(b.x-c.x);
28     return h*i<=eps&&j*k<=eps;
29 };
30 point st[102],en[102];
31 int main()
32 {
33     int n,j,i,cnt=0;
34     while(scanf("%d",&n),n)
35     {
36         cnt=0;
37         for( i=0 ; i<n ; i++ )
38          scanf("%lf%lf%lf%lf",&st[i].x,&st[i].y,&en[i].x,&en[i].y);
39 
40         for( i=0 ; i<n ; i++ )
41         {
42             for(j=i+1 ; j<n ;j++ )
43             {
44                 if(inter(st[i],en[i],st[j],en[j]))
45                     cnt++;
46             }
47         }
48         printf("%d\n",cnt);
49     }
50     return 0;
51 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-01-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • You can Solve a Geometry Problem too
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档