POJ 3304 Segments 基础线段交判断

LINK

题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点

思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交。判断相交先求叉积再用跨立实验。枚举每个线段的起始结束点作为直线起点终点遍历即可。

 

/** @Date    : 2017-07-12 14:35:44
  * @FileName: POJ 3304 基础线段交判断.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

  
struct point  
{  
    double x, y;  
    point(double _x, double _y){x = _x, y = _y;}
    point(){}
    point operator -(const point &b) const
	{
		return point(x - b.x, y - b.y);
	}
	double operator *(const point &b) const 
	{
		return x * b.x + y * b.y;
	}
	double operator ^(const point &b) const
	{
		return x * b.y - y * b.x;
	}
};  
  
struct line
{
	point s, t;
	line(){}
	line(point ss, point tt){s = ss, t = tt;}
};

double cross(point a, point b)
{
	return a.x * b.y - a.y * b.x;
}

double xmult(point p1, point p2, point p0)  
{  
    return (p1 - p0) ^ (p2 - p0);  
}  

double distc(point a, point b)
{
	return sqrt((b - a) * (b - a));
}

bool opposite(point p1, point p2, line l)
{
	double t = xmult(l.s, l.t, p1) * xmult(l.s, l.t, p2);
	printf("%.8lf\n", t);
	return xmult(l.s, l.t, p1) * xmult(l.s, l.t, p2) < -eps;
}

//线段与线段交
bool Sjudgeinter(line a, line b)
{
	return opposite(b.s, b.t, a) && opposite(a.s, a.t, b);
}

int sign(double x)
{
	if(fabs(x) < eps)
		return 0;
	if(x < 0)
		return -1;
	return 1;
}
//线段与直线交 a为直线
bool judgeinter(line a, line b)
{
	//return opposite(b.s, b.t, a);
	/*double x = xmult(a.s, a.t, b.s);
	double y = xmult(a.s, a.t, b.t);
	printf("@%.4lf %.4lf\n", x, y);*/
	return sign(xmult(a.s, a.t, b.s)) * sign(xmult(a.s, a.t, b.t)) <= 0;
}

int n;
point p[200];
line l[200];
bool check(line li)
{
	if(sign(distc(li.s, li.t)) == 0)
		return 0;
	for(int i = 0; i < n; i++)
		if(judgeinter(li, l[i]) == 0)
			return 0;
	return 1;
}

int main()
{
	int T;
	cin >> T;
	while(T--)
	{
		scanf("%d", &n);
		for(int i = 0; i < n; i++)
		{
			double x1, x2, y1, y2;
			scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
			p[i] = point(x1, y1), p[i + 1] = point(x2, y2);
			l[i] = line(p[i], p[i + 1]);
		}
		int ans = 0;
		/*for(int i = 0; i < n * 2; i++)//不知道为啥直接枚举所有点就是WA
		{
			for(int j = 0; j < n * 2; j++)
			{
				if(ans)
					break;
				if(i == j || distc(p[i],p[j]) < eps)
					continue;
				line tmp = line(p[i], p[j]);
				if(p[i].x == p[j].x && p[i].y == p[j].y)//考虑到枚举直线为重合点
					continue;
				int flag = 0;
				for(int k = 0; k < n; k++)
				{
					if(k == 1)
						printf("**");
					if(judgeinter(tmp, l[k]) == 0)
					{
						flag = 1;
						break;
					}

				}
				if(!flag)
					ans = 1;
			cout << i << "~"<< j << endl;
			}
		}*/
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				if(check(line(l[i].s, l[j].s))
				|| check(line(l[i].s,l[j].t))
				|| check(line(l[i].t, l[j].s))
				|| check(line(l[i].t, l[j].t)) )
				{
					ans = 1;
					break;
				}
			}
		}
		printf("%s\n", ans?"Yes!":"No!");
	}
    return 0;
}
//询问是否存在直线,使得所有线段在其上的投影拥有公共点
//如果存在公共区域,对其作垂线,那么其垂线必定过所有的线段
//那么转换为是否存在直线 与所有线段都相交
posted @ 2017-07-16 11:02  Lweleth  阅读(156)  评论(0编辑  收藏  举报