POJ 3304 判断线段 与 直线 相交
2013-08-03 23:42
489 查看
题意:给出n条线段,判断是否存在有一条直线,满足所有的线段在直线上投影后至少有一个公共点(与所有线段都会相交)
开始想错了,因为是再已经给的线段中 是否存在选一条做直线,使得它与所有线段相交;
这样也能过sample input;
害我找了一天了;
开始想错了,因为是再已经给的线段中 是否存在选一条做直线,使得它与所有线段相交;
这样也能过sample input;
害我找了一天了;
#include <iostream> #include <cmath> #include <cstdlib> #include <cstdio> using namespace std; const double eps = 1e-8; const int maxn = 100+5; struct Point { double x; double y; Point(double a = 0, double b = 0){ x = a; y = b; } } P[maxn*2]; struct Line { Point u; Point v; } L; int T,N; int Sig(double x) { return (x > eps) - (x < -eps); // return x < -eps? -1 : x > eps; //这个也行 } double Mult(Point p1, Point p2) // 两点 叉积 { return p1.x*p2.y - p1.y*p2.x; } double Mult(Point p0, Point p1, Point p2) //三点 叉积 { return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y); } int same_site(Point p1, Point p2, Line L) //point p1 & p2 on the same site of line L { int a = Sig(Mult(L.u,L.v,p1)); int b = Sig(Mult(L.u,L.v,p2)); return a*b > 0; } int Judge(Line L) { if(Sig(L.u.x - L.v.x)==0 && Sig(L.u.y - L.v.y)==0) return false; for(int i = 1; i < 2*N; i += 2) if(same_site(P[i],P[i+1],L)) return false; return true; } int slove() { for(int i = 1; i < N*2; i++) { for(int j = i+1; j <= N*2; j++) { L.u.x = P[i].x; L.u.y = P[i].y; L.v.x = P[j].x; L.v.y = P[j].y; if(Judge(L))//如果成功,即找到了这条直线 return true; } } return false; } int main() { #ifndef ONLINE_JUDGE freopen("in","r",stdin); #endif cin>>T; while(T--) { cin>>N; for(int i = 1; i <= N*2; i++) { cin>>P[i].x>>P[i].y; } if(slove()) puts("Yes!"); else puts("No!"); } }
相关文章推荐
- poj 3304 Segments(叉积+直线和线段相交判断)
- POJ 3304 Segments 判断直线和线段相交
- poj 3304 判断直线与线段相交
- POJ 3304 Segments (计算几何、判断直线与线段是否相交)
- POJ 3304 Segments 判断直线与线段相交
- POJ 3304(计算几何初步——判断线段和直线相交,加上枚举)
- POJ 3304 直线与线段相交的判断
- poj 3304 Segments 【判断是否存在一条直线与所有线段相交】
- POJ - 3304 :Segments__判断直线和线段是否 相交
- POJ 3304 判断直线与线段相交
- POJ 3304 判断线段和直线相交
- POJ 3304 Segments (直线和线段相交判断)
- POJ 3304 Segments (直线和线段相交判断)
- POJ 3304 Segments [判断线段和直线相交]
- POJ 3304 Segments(判断线段和直线是否相交)
- 【POJ - 3304 Segments】 直线线段相交判断
- poj 3304 判断是否存在一条直线与所有线段相交
- POJ 3304 Segments (判断直线和线段是否相交)
- poj 3304 Segments(判断直线与线段相交)
- POJ 3304 Segments(判断直线和线段相交)