zoj 1472 Overlapping Shapes

这题。。。再一次说明我没好好读题!!!!

昨晚一直WA得莫名其妙,觉得自己思路哪的完全正确就是不知道哪WA了,无奈搜题解,还搜不到,今早找到了个,我测试他的和我的,发现他的结果不对啊,正纳闷呢(我都想给管理员发邮件了都= =)一看人家输入,丫丫的,圆的话输入的第一个是半径!!!!我看都没看以为先是x,y坐标!!!坑姐啊啊啊!!诡异的是我样例还都过了!!!

改后,瞬秒掉 = =T T。。。笨蛋小媛。。


这题就是判断圆和矩形 圆和圆 矩形和矩形 是否相交 相切也算。所以要考虑的就是,圆和矩形的一个顶点有交点,圆之间内外切,矩形与矩形重合一个点之类的。


我判断圆和矩形重叠的方法是,如果矩形四个点都在圆内部,不符合,如果不都在圆内部,判断圆心到矩形每条边的最短距离,如果都大于半径,不符合,剩下的就是符合的了。


这题又让我WA了一版,情何堪!!!

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>

using namespace std;

struct point{ double x,y;};
struct circle{ point a; double r;};
struct rectrangle{ point p[4];};
struct polygon{ bool flag; circle c; rectrangle r;};
polygon a,b;
const double eps = 1e-6;
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
double disp2p(point a,point b) 
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
void inputc(polygon &a,double x,double y,double r)
{
	a.flag = true;
	a.c.a.x = x; a.c.a.y = y; a.c.r = r;
}
void inputr(polygon &b,double xmin,double ymax,double xmax,double ymin)
{
	b.flag = false;
	b.r.p[0].x = b.r.p[1].x = xmin;
	b.r.p[0].y = b.r.p[3].y = ymin;
	b.r.p[2].x = b.r.p[3].x = xmax;
	b.r.p[1].y = b.r.p[2].y = ymax;
}
void input(polygon &a)
{
	char str[15];
	scanf("%s",&str);
	double x,y,z,p;
	if( strcmp(str,"circle") == 0 )
	{
		scanf("%lf%lf%lf",&x,&y,&z);
		inputc(a,y,z,x);
	}
	else
	{
		scanf("%lf%lf%lf%lf",&x,&y,&z,&p);
		if( xy(z,x) ) swap(x,z);
		if( xy(y,p) ) swap(y,p);
		inputr(a,x,y,z,p);
	}
}
bool c2c_inst(point a,double r1,point b,double r2)
{
	if( xyd(disp2p(a,b),r1+r2) && dyd(disp2p(a,b),fabs(r1 - r2)) )
		return true;
	return false;
}
bool c2c(circle a,circle b)
{
	return c2c_inst(a.a,a.r,b.a,b.r);
}
double disp2seg(point p,point l1,point l2)
{
	point t = p;
	t.x += l1.y - l2.y;
	t.y += l2.x - l1.x;
	if( dyd(crossProduct(l1,t,p)*crossProduct(l2,t,p),0.0) )
		return xy(disp2p(p,l1),disp2p(p,l2)) ? disp2p(p,l1) : disp2p(p,l2);
	return fabs( crossProduct(p,l1,l2) )/disp2p(l1,l2);
}
bool c2r(circle a,rectrangle b)
{
	bool flag = false;
	for(int i=0; i<4; i++)
		if( dyd(disp2p(a.a,b.p[i]),a.r) )
			flag = true;
	if( !flag ) return false;
	flag = false;
	for(int i=0; i<4; i++)
		if( xyd(disp2seg(a.a,b.p[i],b.p[(i+1)%4]),a.r) )
			flag = true;
	if( !flag ) return false;
	return true;
}
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( dd(crossProduct(a,b,c),0.0) && dyd(c.x,minx) && xyd(c.x,maxx) && dyd(c.y,miny) && xyd(c.y,maxy) )
		return true;
	return false;
}
bool s2s_inst(point p1,point p2, point p3, point p4) 
{
	double d1 = crossProduct(p3,p4,p1);
	double d2 = crossProduct(p3,p4,p2);
	double d3 = crossProduct(p1,p2,p3);
	double d4 = crossProduct(p1,p2,p4);
	if( xy(d1 * d2,0.0) && xy(d3 * d4,0.0) )	return true;
	if( dd(d1,0.0) && onSegment(p3,p4,p1) )		return true;//如果不判端点相交,则下面这四句话不需要 
	if( dd(d2,0.0) && onSegment(p3,p4,p2) )		return true;
	if( dd(d3,0.0) && onSegment(p1,p2,p3) )		return true;
	if( dd(d4,0.0) && onSegment(p1,p2,p4) )		return true;
	return false;
}
bool r2r(rectrangle a,rectrangle b)
{
	for(int i=0; i<4; i++)
		for(int k=0; k<4; k++)
			if( s2s_inst(a.p[i],a.p[(i+1)%4],b.p[k],b.p[(k+1)%4]) )
				return true;
	return false;
}

int main()
{
	int ncases;

	while( ~scanf("%d",&ncases) )
	while( ncases-- )
	{
		input(a);
		input(b);
		bool ans;
		if( a.flag && b.flag )		ans = c2c(a.c,b.c);
		if( a.flag && !b.flag )		ans = c2r(a.c,b.r);
		if( !a.flag && b.flag )		ans = c2r(b.c,a.r);
		if( !a.flag && !b.flag )	ans = r2r(a.r,b.r);
		
		if( ans )
			printf("yes\n");
		else
			printf("no\n");
	}
return 0;
}


你可能感兴趣的:(c,struct,测试,input,Shapes)