HDUOJ1086You can Solve a Geometry Problem too
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
线段是否相交的判断,采用的石墨板,..
代码:
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("%dn",cnt);
49 }
50 return 0;
51 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- laravel框架模型、视图与控制器简单操作示例
- Laravel关系模型指定条件查询方法
- 在laravel中使用with实现动态添加where条件
- laravel Model 执行事务的实现
- 在laravel中实现事务回滚的方法
- thinkphp5框架实现数据库读取的数据转换成json格式示例
- phpfpm的作用和用法
- 浅谈PHP5.6 与 PHP7.0 区别
- laravel执行php artisan migrate报错的解决方法
- Laravel统计一段时间间隔的数据方法
- laravel withCount 统计关联数量的方法
- 使用laravel和ECharts实现折线图效果的例子
- Laravel 将数据表的数据导出,并生成seeds种子文件的方法
- Laravel Eloquent ORM 多条件查询的例子
- laravel5.5添加echarts实现画图功能的方法