您的位置:首页 > 其它

hdu 1086 You can Solve a Geometry Problem too

2011-04-07 19:51 337 查看
求出线段相交的对数



啦啦~~看算导的~~~我觉得计算几何蛮有意思的哇~~~不解释啦~~



#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef struct Point{
	double x,y;
}Point;
typedef struct Line{
	Point a,b;
}Line;
double Direction(Point a, Point b, Point c )
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool Onsegment(Point a, Point b, Point c)
{
	double maxx = max(a.x,b.x);
	double maxy = max(a.y,b.y);
	double minx = min(a.x,b.x);
	double miny = min(a.y,b.y);
	if( c.x >= minx && c.x <= maxx && c.y >= miny && c.y <= maxy )
		return true;
	return false;
}
bool segIntersect(Point p1,Point p2, Point p3, Point p4)
{
	double d1 = Direction(p3,p4,p1);
	double d2 = Direction(p3,p4,p2);
	double d3 = Direction(p1,p2,p3);
	double d4 = Direction(p1,p2,p4);
	if( d1 * d2 < 0 && d3 * d4 < 0 )
		return true;
	if( d1 == 0 && Onsegment(p3,p4,p1) )
		return true;
	if( d2 == 0 && Onsegment(p3,p4,p2) )
		return true;
	if( d3 == 0 && Onsegment(p1,p2,p3) )
		return true;
	if( d4 == 0 && Onsegment(p1,p2,p4) )
		return true;
	return false;
}
Line l[110];
int main()
{
	int n;
	int sum;
	while( ~scanf("%d",&n) && n )
	{
		sum = 0;
		for(int i=0; i<n; i++)
			scanf("%lf %lf %lf %lf",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y);
		for(int i=0; i<n; i++)
			for(int k=i+1; k<n; k++)
				if( segIntersect(l[i].a,l[i].b,l[k].a,l[k].b) )
					sum++;
		printf("%d/n",sum);
	}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: