HDU 1086 - You can Solve a Geometry Problem too
2016-07-27 22:30
267 查看
判断线段两两相交的个数
打模板熟练程度++;
打模板熟练程度++;
#include <iostream> #include <cstdio> #include <cmath> using namespace std; const double EPS = 1e-10; struct Point { double x,y; Point(){} Point(double x1,double y1):x(x1),y(y1) {} }a[105],b[105]; typedef Point Vect; Vect operator - (Vect A, Vect B) { return Vect(A.x - B.x, A.y - B.y); } int dcmp(double x) { return fabs(x) < EPS ? 0 : (x < 0? -1: 1); } double Cross(Vect A, Vect B) { return A.x * B.y - A.y * B.x; } double Dot(Vect A, Vect B) { return A.x * B.x + A.y * B.y; } bool OnSegment(Point p, Point a1, Point a2) { return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp( Dot(a1 - p, a2 - p) ) <= 0; } bool SegmentIntersection(Point a1, Point a2, Point b1, Point b2) { double c1 = Cross(a2 - a1, b1 - a1); double c2 = Cross(a2 - a1, b2 - a1); double c3 = Cross(b2 - b1, a1 - b1); double c4 = Cross(b2 - b1, a2 - b1); if(dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0) return 1; else if(OnSegment(b1, a1, a2) ) return 1; else if(OnSegment(b2, a1, a2) ) return 1; else if(OnSegment(a1, b1, b2) ) return 1; else if(OnSegment(a2, b1, b2) ) return 1; else return 0; } int main() { int n; while(~scanf("%d",&n) && n) { for(int i = 1; i <= n; i++) scanf("%lf%lf%lf%lf", &a[i].x, &a[i].y, &b[i].x, &b[i].y); int ans = 0; for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) if(SegmentIntersection(a[i], b[i], a[j], b[j])) ans++; printf("%d\n", ans); } }
相关文章推荐
- iOS面试题,看看你究竟知道多少(二)
- ViewPager无限循环滑动无卡顿,详解OnPageChangeListener
- Linux常用命令
- 2016 多校联训 第三场Teacher Bo hdoj5762
- Gradle 详解
- Java面向对象二
- day07_xml学习
- Retrofit2使用CallAdapterFactory的错误处理
- 新建虚拟目录和删除虚拟目录
- 设计模式10_门面模式
- Http协议备忘录
- iOS --- 自定义navigationBar的两种常见方式
- Linux基础知识
- linux简单常用命令及个人总结
- 关于新导入java项目后提示:Configure build path
- 统计0到n之间1的个数
- [CF Gym 100827F] Knights [2014-2015 ACM-ICPC Pacific Northwest Regional Contest F]
- vs运行时出现的问题及其解决方法1.1——1.10
- 机器学习第2周编程作业
- android UI基本控件