Hdu 1086 You can Solve a Geometry Problem too[判断线段相交,完整版]
来源:互联网 发布:淘宝零食推荐知乎健康 编辑:程序博客网 时间:2024/05/02 09:58
题目链接:点击打开链接
题目的意思很简单,就是很多线段的交点有多少。算的留下一个模板吧。
code:
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int N = 1e2 + 5;struct Point{ double x, y;};struct Vector{ double x, y;};struct Line{ Point a, b;}A[N];double operator *(Vector a, Vector b){ return a.x * b.y - b.x * a.y;}Vector operator - (Point a, Point b){//计算向量ab Vector tmp; tmp.x = b.x - a.x; tmp.y = b.y - a.y; return tmp;}bool OnLine(Line ab, Point c){//判断c是否在线段ab上 int maxx = max(ab.a.x, ab.b.x); int minx = min(ab.a.x, ab.b.y); int maxy = max(ab.a.y, ab.b.y); int miny = min(ab.a.y, ab.b.y); if(c.x >= minx && c.x <= maxx && c.y >= miny && c.y <= maxy) return true; return false;}bool Judge(Line X, Line Y){ Vector a, b, c, a1, b1, c1; a = X.a - X.b; b = Y.a - X.b; c = Y.b - X.b; a1 = Y.a - Y.b; b1 = X.a - Y.b; c1 = X.b - Y.b; double dir1 = a * b, dir2 = a * c, dir3 = a1 * b1, dir4 = a1 * c1; if(dir1 * dir2 < 0 && dir3 * dir4 < 0) return true; else if(dir1 == 0 && OnLine(X, Y.a)) return true; else if(dir2 == 0 && OnLine(X, Y.b)) return true; else if(dir3 == 0 && OnLine(Y, X.a)) return true; else if(dir4 == 0 && OnLine(Y, X.b)) return true; else return false;}int main(){ int n; while(scanf("%d", &n) && n){ for(int i = 1; i <= n; i ++){ scanf("%lf %lf %lf %lf",&A[i].a.x, &A[i].a.y, &A[i].b.x, &A[i].b.y); } int ans = 0; for(int i = 1; i < n; i ++){ for(int j = i + 1; j <= n; j ++){ if(Judge(A[i], A[j])) ans ++; } } printf("%d\n", ans); } 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 ACM 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 You can Solve a Geometry Problem too(判断两条线段相交)
- hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交
- 杭电 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(简单的线段相交)
- hdu 1086 You can Solve a Geometry Problem too(求线段相交点个数 模板)
- hdu 1086 You can Solve a Geometry Problem too(几何(线段相交))
- unity程序材质ProceduralMaterial使用
- 黑马程序员:File类
- servlet中常见的问题与解答
- ini文件解析器 源代码阅读以及测试例子
- 图的表示及优先遍历
- Hdu 1086 You can Solve a Geometry Problem too[判断线段相交,完整版]
- 20140717 随笔 技术文章安家至CSDN
- 【神经网络学习笔记】遗传算法初探
- 10 HashSet HashMap源码 Properties
- Java swing 实现高仿 QQ2013之登入界面设计
- UVa10273 Eat or Not to Eat?
- php——学习笔记,赋值运算符和递增或递减运算符和位运算符
- Tkinter中的Events事件(关于bind()的详细描述)
- Dividing - POJ 1014 dp背包