Poj 3449

来源:互联网 发布:加工贸易方式数据 编辑:程序博客网 时间:2024/05/16 08:28

判断平面多边形相交,输入写复杂了 >.< 注意输出格式即可


#include <iostream>#include <algorithm>#include <string> #include <vector>#include <set>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#define MAX(a, b) (a > b ? a : b)#define MIN(a, b) (a < b ? a : b)using namespace std;const double pi = acos(-1);const double eps = 1e-8;class Point{public:double x, y;Point( const double &a = 0, const double &b = 0) : x(a), y(b){}Point operator - (const Point &k){return Point( this->x - k.x, this->y - k.y);}Point operator + (const Point &k){return Point( this->x + k.x, this->y + k.y);}double operator ^ (const Point &k){return this->x * k.y - this->y * k.x;}void input(){scanf(" (%lf, %lf)", &x, &y);}Point turn( const double &k){return Point(x * cos(k) - y * sin(k),x * sin(k) + y * cos(k));}};bool isCrossed( Point &A, Point &B, Point &C, Point &D){return((MAX(A.x, B.x) >= MIN(C.x, D.x)) &&(MAX(A.y, B.y) >= MIN(C.y, D.y)) &&(MIN(A.x, B.x) <= MAX(C.x, D.x)) &&(MIN(A.y, B.y) <= MAX(C.y, D.y)) &&(((A - C) ^ (A - D)) * ((B - C) ^ (B - D))) <= 0 && (((C - A) ^ (C - B)) * ((D - A) ^ (D - B))) <= 0);}class Poly{public:int num;vector<Point> lib;char name;void input(const int &n){Point p;num = n;for(int i = 0; i < n; ++i){scanf(" (%lf,%lf)", &p.x, &p.y);lib.push_back( p );}}bool isInter( Poly &k){bool answer = true;for(int i = 0; i < this->num && answer; ++i){for(int j = 0; j < k.num && answer; ++j){answer = !( isCrossed( this->lib[ i ], this->lib[ (i + 1) % this->num], k.lib[j], k.lib[ (j + 1) % k.num]) );}}return !answer;}bool operator < (const Poly &k) const{return this->name < k.name;}};char s[20];set<char> answer;set<char> :: iterator it3;vector<Poly> vec;vector<Poly> :: iterator it, it2;void solve(){char c;Poly now;int k;bool flag2 = false;vec.clear();while( scanf("%c", &c) == 1 && c != '.'){if(c == '-'){if(flag2)printf("\n");elseflag2 = true;sort(vec.begin(), vec.end());for(it = vec.begin(); it != vec.end(); ++it){bool flag = false;answer.clear();for( it2 = vec.begin(); it2 != vec.end(); ++it2){if(it != it2){if( it->isInter( *it2) ){answer.insert( it2 -> name );flag = true;}}}if(flag){printf("%c intersects with", it->name);if(answer.size() > 2){while( answer.size() > 1){printf(" %c,", *(answer.begin()) );answer.erase( answer.begin() );}printf(" and %c\n", *(answer.begin()));}else{printf(" %c", *(answer.begin()));answer.erase( answer.begin() );if(!answer.empty())printf(" and %c", *(answer.begin()));printf("\n");}}else{printf("%c has no intersections\n", it->name);}}vec.clear();}else{now.lib.clear();now.num = 0;scanf(" %s", s);now.name = c;if( strcmp( s, "square") == 0){Point cache[4], a;cache[0].input();cache[2].input();a.x = ( cache[0].x + cache[2].x) / 2.0;a.y = ( cache[0].y + cache[2].y) / 2.0;Point v = cache[2] - cache[0];v = v.turn( pi / 2.0 );v.x /= 2.0;v.y /= 2.0;cache[1] = a + v;cache[3] = a - v;now.num = 4;now.lib.clear();for(int i = 0; i < 4; ++i)now.lib.push_back( cache[i] );}else if( strcmp( s, "rectangle") == 0){now.input( 3 );Point b = now.lib[2] - now.lib[1];now.lib.push_back( (now.lib[0] + b) );now.num = 4;}else if( strcmp( s, "triangle") == 0){now.input( 3 );}else if( strcmp( s, "line") == 0){now.input( 2 );}else if( strcmp( s, "polygon") == 0){scanf("%d", &k);now.input( k );}vec.push_back( now );}scanf("\n");}}int main(){solve();return 0;}


0 0
原创粉丝点击