poj 3082多边形相交 'Roid Rage

时间:2023-03-10 02:25:15
poj  3082多边形相交 'Roid Rage

题意是判断多边形是否相交

主要的思路就是判断每一个点是否在另外的多变形内

判断一个点是否在另一个多边形内主要思路是:

判断的那个点向左边做射线,如果射线与多边形的交点为奇数个则在多边形内,偶数个则不在,其中有特殊情况:

1.如果判断的点与所要判断的边在平行且在所要判断的边上,则在多边形上

2.如果向左做射线恰好在某一点上,不特殊处理会计算两次,因为在两条边上,判断射线与多变形的交点数目,所以要在一个情况下忽略

3.就是判断点是否在多边形的左边了

but:WA了好久因为一个多边形可能包涵另一个多边形所以要全部遍历^!!!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <cctype>
#include <set>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
const int maxn = ;
typedef long long LL;
struct Point{
double x,y;
};
using namespace std;
Point point[][];
int t[];
bool flag[][]; //参数:
// POINT p 指定的某个点
// LPPOINT ptPolygon 多边形的各个顶点坐标(首末点可以不一致)
// int nCount 多边形定点的个数
bool PtInPolygon (Point p, int num)
{
int nCross = ;
for(int i = ; i < t[num]; i++){
Point p1 = point[num][i];
int tt = (i+) % t[num];
Point p2 = point[num][tt];
double x1 = min(p1.x,p2.x);
double x2 = max(p1.x,p2.x);
double y1 = min(p1.y,p2.y);
double y2 = max(p1.y,p2.y);
if((p.y-p1.y)*(p.x-p2.x) == (p.y-p2.y) * (p.x - p1.x)){
if(p.x >= x1 && p.x <= x2 && p.y >= y1 && p.y <= y2)
return true;
continue;
}
if(p.y < y1)
continue;
if(p.y > y2 || abs(p.y-y2) < 10e-)
continue;
double x = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
if ( x > p.x )
nCross++;
}
return (nCross % == );
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif
int tt;
cin >> tt;
for(int cas = ;cas <= tt;cas++){
int n;
cin >> n;
memset(flag,,sizeof(flag));
for(int i = ;i < n;i++){
cin >> t[i];
for(int j = ;j < t[i];j++){
int tmp1,tmp2;
char ch;
cin >> tmp1 >> ch >> tmp2;
point[i][j].x = tmp1;
point[i][j].y = tmp2;
}
}
for(int i = ;i < n;i++){
for(int j = ;j < n;j++){
if(i != j){
bool mark = ;
for(int k = ;k < t[i];k++){
if(PtInPolygon(point[i][k],j)){
mark = ;
break;
}
}
if(mark){
flag[i][j] = flag[j][i] = ;
}
}
}
}
bool aflag = ;
cout << "Data Set #" << cas << endl;
for(int i = ;i < n;i++){
for(int j = ;j < n;j++){
if(flag[i][j]){
flag[i][j] = flag[j][i] = ;
aflag = ;
cout << i+ << " " << j+<< endl;
}
}
}
if(aflag)
cout << "no collisions" << endl;
}
return ;
}

Code

测试用例:


 , , , ,
, , , , , , ,
, , ,
, , , , ,
, , , , , , , , , ,
, , ,
, , , , , ,
, , , , , , ,
, , , ,
, , , ,
, , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , ,
, , , , , , , ,
, , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , ,
, , , ,

ans:

Data Set #
no collisions
Data Set # Data Set #
no collisions
Data Set # Data Set #
no collisions
Data Set # Data Set #
no collisions
Data Set # Data Set # Data Set # Data Set # Data Set # Data Set # Data Set #
no collisions