HDU-1086-You can Solve a Geometry Problem too
来源:互联网 发布:数据集成论文 编辑:程序博客网 时间:2024/06/05 11:20
You can Solve a Geometry Problem too
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.
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.
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
20.00 0.00 1.00 1.000.00 1.00 1.00 0.0030.00 0.00 1.00 1.000.00 1.00 1.00 0.0000.00 0.00 1.00 0.000
Sample Output
13
题中输入的就是两点的坐标,所以我们用这两点的坐标来求直线方程
已知两点(x1,y1) , (x2,y2)
代入两点公式: (x-x1)/(x2-x1)=(y-y1)/(y2-y1)
整理可得方程: H = x * (y2 - y1 ) - y * ( x2 - x1 ) - x1 * y2 + x2 * y1;然后将另一条线段的两个端点分别带入方程中 可计算出一个结果 (有正负性),在直线上方的点带入方程会得到一个正值,而在直线下方的点带入方程会得到一个负值,so。。。只要两个值异号便可说明这两条线线段有交点。
需要注意的是:要分别判断,意思就是有两条线段 a,b 既要判断 b的两个端点是否在a的两侧,还要判断a的两个端点是否在b的两个端点。。就OK 了。。
PS:感谢轩仔。。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;struct node{ double x; double y;} a[200],b[200];int pd ( node a1, node b1,node a2,node b2){ double h1 = a2.x*(b1.y - a1.y) - a2.y*(b1.x-a1.x) - a1.x*b1.y + b1.x*a1.y; double h2 = b2.x*(b1.y - a1.y) - b2.y*(b1.x-a1.x) - a1.x*b1.y + b1.x*a1.y; //printf ("%lf %lf\n",h1,h2); if ( h1 * h2 <= 0) { return 1; } else return 0;}int main (){ int n; while (~scanf ("%d",&n)&&n) { int sum = 0; for (int i = 0; i < n; i++) { scanf ("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y); } for (int i = 0 ; i < n; i++) { for (int j = i + 1; j < n; j++) { if (pd(a[i],b[i],a[j],b[j]) && pd (a[j],b[j],a[i],b[i])) { sum++; } } } printf ("%d\n",sum); } return 0;}
0 0
- hdu 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- hdu/hdoj 1086 You can Solve a Geometry Problem too
- hdu-1086You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- HDU 1086:You can Solve a Geometry Problem too
- HDU 1086You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- HDU-1086-You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- HDU-1086 You can Solve a Geometry Problem too
- HDU-1086-You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- oc 多继承
- win7中将文件拷贝到虚拟机linux下的方法
- 短期理财产品选购攻略及风险提示
- 《STL源码剖析》---stl_stack.h阅读笔记
- 两个不错的bootstrap风格的select模拟插件
- HDU-1086-You can Solve a Geometry Problem too
- HDU 4937 Lucky Number 【搜索】
- android开源项目总结
- TP-Link WR842N VPN错误619 不能建立到远程计算机的连接
- C/C++网络资源
- PLSQL Developer远程连接oracle数据库经验
- css基础01
- HDOJ 4937 Lucky Number
- plupload 上传组件,后台用java实现