UVA_11343_Isolated Segments

#include<iostream>  
#include<sstream>  
#include<string>  
#include<vector>  
#include<list>  
#include<set>  
#include<map>  
#include<stack>  
#include<queue>  
#include<algorithm>  
#include<numeric>  
#include<cmath>  
#pragma warning(disable:4996)  
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
using std::swap;
using std::min;
using std::max;
class Point 
{
public:
	double x,y; 
};
const double eps = 1e-20;
bool isEqual(const double &a,const double &b) 
{
	return (abs(a - b) < eps);
}
//判断两点是否相等
bool operator==(const Point &p1, const Point &p2) 
{
	return (isEqual(p1.x, p2.x) && isEqual(p1.y, p2.y));
}
//比较两点坐标大小,先比较x坐标,若相同则比较y坐标
bool operator>(const Point &p1, const Point &p2) 
{
	return (p1.x > p2.x || (isEqual(p1.x, p2.x) && p1.y > p2.y));
}
//计算两向量外积(叉乘)
double operator^(const Point &p1, const Point &p2)
{
	return (p1.x * p2.y - p1.y * p2.x);
}
//判定两线段位置关系,并求出交点(如果存在)。返回值列举如下:
//[有重合] 完全重合(6),1个端点重合且共线(5),部分重合(4)
//[无重合] 两端点相交(3),交于线上(2),正交(1),无交(0),参数错误(-1)
int Intersection(Point p1, Point p2, Point p3, Point p4, Point &point) 
{
	//保证参数p1!=p2,p3!=p4
	if (p1 == p2 || p3 == p4)
	{
		return -1; //返回-1代表至少有一条线段首尾重合,不能构成线段
	}
	//为方便运算,保证各线段的起点在前,终点在后。
	if (p1 > p2) 
	{
		swap(p1, p2);
	}
	if (p3 > p4) 
	{
		swap(p3, p4);
	}
	//判定两线段是否完全重合
	if (p1 == p3 && p2 == p4) 
	{
		return 6;
	}
	//求出两线段构成的向量
	Point v1 = { p2.x - p1.x, p2.y - p1.y }, v2 = { p4.x - p3.x, p4.y - p3.y };
	//求两向量外积,平行时外积为0
	double Cross = v1 ^ v2;
	//如果起点重合
	if (p1 == p3) 
	{
		point = p1;
		//起点重合且共线(平行)返回5;不平行则交于端点,返回3
		return (isEqual(Cross, 0) ? 5 : 3);
	}
	//如果终点重合
	if (p2 == p4)
	{
		point = p2;
		//终点重合且共线(平行)返回5;不平行则交于端点,返回3
		return (isEqual(Cross, 0) ? 5 : 3);
	}
	//如果两线端首尾相连
	if (p1 == p4) 
	{
		point = p1;
		return 3;
	}
	if (p2 == p3) {
		point = p2;
		return 3;
	}
	//经过以上判断,首尾点相重的情况都被排除了
	//将线段按起点坐标排序。若线段1的起点较大,则将两线段交换
	if (p1 > p3) {
		swap(p1, p3);
		swap(p2, p4);
		//更新原先计算的向量及其外积
		swap(v1, v2);
		Cross = v1 ^ v2;
	}
	//处理两线段平行的情况
	if (isEqual(Cross, 0)) 
	{
		//做向量v1(p1, p2)和vs(p1,p3)的外积,判定是否共线
		Point vs = { p3.x - p1.x, p3.y - p1.y };
		//外积为0则两平行线段共线,下面判定是否有重合部分
		if (isEqual(v1 ^ vs, 0)) {
			//前一条线的终点大于后一条线的起点,则判定存在重合
			if (p2 > p3) {
				point = p3;
				return 4; //返回值4代表线段部分重合
			}
		}//若三点不共线,则这两条平行线段必不共线。
		 //不共线或共线但无重合的平行线均无交点
		return 0;
	} //以下为不平行的情况,先进行快速排斥试验
	  //x坐标已有序,可直接比较。y坐标要先求两线段的最大和最小值
	double ymax1 = p1.y, ymin1 = p2.y, ymax2 = p3.y, ymin2 = p4.y;
	if (ymax1 < ymin1) {
		swap(ymax1, ymin1);
	}
	if (ymax2 < ymin2) {
		swap(ymax2, ymin2);
	}
	//如果以两线段为对角线的矩形不相交,则无交点
	if (p1.x > p4.x || p2.x < p3.x || ymax1 < ymin2 || ymin1 > ymax2) {
		return 0;
	}//下面进行跨立试验
	Point vs1 = { p1.x - p3.x, p1.y - p3.y }, vs2 = { p2.x - p3.x, p2.y - p3.y };
	Point vt1 = { p3.x - p1.x, p3.y - p1.y }, vt2 = { p4.x - p1.x, p4.y - p1.y };
	double s1v2, s2v2, t1v1, t2v1;
	//根据外积结果判定否交于线上
	if (isEqual(s1v2 = vs1 ^ v2, 0) && p4 > p1 && p1 > p3) {
		point = p1;
		return 2;
	}
	if (isEqual(s2v2 = vs2 ^ v2, 0) && p4 > p2 && p2 > p3) {
		point = p2;
		return 2;
	}
	if (isEqual(t1v1 = vt1 ^ v1, 0) && p2 > p3 && p3 > p1) {
		point = p3;
		return 2;
	}
	if (isEqual(t2v1 = vt2 ^ v1, 0) && p2 > p4 && p4 > p1) {
		point = p4;
		return 2;
	} //未交于线上,则判定是否相交
	if (s1v2 * s2v2 > 0 || t1v1 * t2v1 > 0) {
		return 0;
	} //以下为相交的情况,算法详见文档
	  //计算二阶行列式的两个常数项
	double ConA = p1.x * v1.y - p1.y * v1.x;
	double ConB = p3.x * v2.y - p3.y * v2.x;
	//计算行列式D1和D2的值,除以系数行列式的值,得到交点坐标
	point.x = (ConB * v1.x - ConA * v2.x) / Cross;
	point.y = (ConB * v1.y - ConA * v2.y) / Cross;
	//正交返回1
	return 1;
}
bool in(const double &value,const double &lower_bound,const double&higher_bound)
{
	if (value-lower_bound>eps&&higher_bound-value>eps)
	{
		return true;
	}
	return false;
}
//主函数
int main()
{
	//freopen("input.txt", "r", stdin);  
	//freopen("output.txt", "w", stdout);  
	int T;
	while (cin >> T)
	{
		while (T--)
		{
			int n; cin >> n;
			vector<pair<pair<double,double>,pair<double,double>>>line;
			for (int i = 0; i < n; i++)
			{
				long double x1, x2, y1, y2;
				cin >> x1 >> y1 >> x2 >> y2;
				line.push_back({ { x1,y1 },{ x2,y2 } });
			}
			vector<bool>collided(n);
			for (int i = 0; i<n; i++)
			{
				for (int j = i + 1; j < n; j++)
				{
					Point point;
					auto type = Intersection({line[i].first.first,line[i].first.second}, { line[i].second.first,line[i].second.second }, { line[j].first.first,line[j].first.second }, { line[j].second.first,line[j].second.second },point);
					if (type>0)
					{
						collided[i] = collided[j] = true;
					}
				}
			}
			cout << std::count(collided.begin(), collided.end(), false) << endl;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值