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 }
- Nodejs学习笔记(七)--- Node.js + Express 构建网站简单示例
- 如何写出好代码
- Nodejs学习笔记(十五)--- Node.js + Koa2 构建网站简单示例
- WCF后续之旅(15): 逻辑地址和物理地址
- 深入理解string和如何高效地使用string
- Nodejs学习笔记(四)——支持Mongodb
- 语音识别类产品的分类及应用场景
- 小程序新能力-个人开发者尝鲜微信小程序
- 径向基神经网络续1
- 打造你的专属AI游戏机器人:太空侵略者
- WCF后续之旅(16): 消息是如何分发到Endpoint的--消息筛选(Message Filter)
- 最高大上的展览!腾讯建了一个小“方盒子”,里面全是高科技
- Blend基础-布局控件
- Spring实战——缓存
- 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 数组属性和方法
- 手写一个Redux,深入理解其原理
- 手写一个React-Redux,玩转React的Context API
- 使用React-Router实现前端路由鉴权
- 手写React-Router源码,深入理解其原理
- 深入Node.js的模块加载机制,手写require函数
- Redux异步解决方案之Redux-Thunk原理及源码解析
- MySQL设计与优化
- Elasticsearch学习笔记
- 《springboot实战》
- 360全国大学生信安技术大赛记录
- gh0st源码分析与远控的编写(四)
- 记一次Elasticsearch优化总结
- unix环境高级编程(上)-文件篇
- php webshell下直接反弹shell(不借助任何其他语言)
- XssHtml – 基于白名单的富文本XSS过滤类