poj 3082多边形相交 'Roid Rage
生活随笔
收集整理的這篇文章主要介紹了
poj 3082多边形相交 'Roid Rage
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題意是判斷多邊形是否相交
主要的思路就是判斷每一個(gè)點(diǎn)是否在另外的多變形內(nèi)
判斷一個(gè)點(diǎn)是否在另一個(gè)多邊形內(nèi)主要思路是:
判斷的那個(gè)點(diǎn)向左邊做射線(xiàn),如果射線(xiàn)與多邊形的交點(diǎn)為奇數(shù)個(gè)則在多邊形內(nèi),偶數(shù)個(gè)則不在,其中有特殊情況:
1.如果判斷的點(diǎn)與所要判斷的邊在平行且在所要判斷的邊上,則在多邊形上
2.如果向左做射線(xiàn)恰好在某一點(diǎn)上,不特殊處理會(huì)計(jì)算兩次,因?yàn)樵趦蓷l邊上,判斷射線(xiàn)與多變形的交點(diǎn)數(shù)目,所以要在一個(gè)情況下忽略
3.就是判斷點(diǎn)是否在多邊形的左邊了
?
but:WA了好久因?yàn)橐粋€(gè)多邊形可能包涵另一個(gè)多邊形所以要全部遍歷^!!!
#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 = 10005; typedef long long LL; struct Point{double x,y; }; using namespace std; Point point[12][22]; int t[12]; bool flag[21][21];//參數(shù): // POINT p 指定的某個(gè)點(diǎn) // LPPOINT ptPolygon 多邊形的各個(gè)頂點(diǎn)坐標(biāo)(首末點(diǎn)可以不一致) // int nCount 多邊形定點(diǎn)的個(gè)數(shù) bool PtInPolygon (Point p, int num) {int nCross = 0;for(int i = 0; i < t[num]; i++){Point p1 = point[num][i];int tt = (i+1) % 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-6)continue;double x = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;if ( x > p.x )nCross++;}return (nCross % 2 == 1); } int main() { #ifndef ONLINE_JUDGEfreopen("in.in","r",stdin); #endifint tt;cin >> tt;for(int cas = 1;cas <= tt;cas++){int n;cin >> n;memset(flag,0,sizeof(flag));for(int i = 0;i < n;i++){cin >> t[i];for(int j = 0;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 = 0;i < n;i++){for(int j = 0;j < n;j++){if(i != j){bool mark = 0;for(int k = 0;k < t[i];k++){if(PtInPolygon(point[i][k],j)){mark = 1;break;}}if(mark){flag[i][j] = flag[j][i] = 1;}}}}bool aflag = 1;cout << "Data Set #" << cas << endl;for(int i = 0;i < n;i++){for(int j = 0;j < n;j++){if(flag[i][j]){flag[i][j] = flag[j][i] = 0;aflag = 0;cout << i+1 << " " << j+1<< endl;}}}if(aflag)cout << "no collisions" << endl;}return 0; } Code測(cè)試用例:
14 2 4 0,0 1,0 1,1 0,1 4 2,2 3,2 3,3 2,3 4 3 2,1 1,2 2,3 3 2,1 3,2 2,3 5 2,0 4,2 2,4 5,4 5,0 4 3,3 1,3 1,5 3,5 1 3 0,0 1,1 1,0 3 3 0,0 1,1 2,0 3 2,0 0,0 1,1 3 4,4 3,3 5,4 2 3 0,0 1,1 2,0 3 4,4 3,3 5,4 10 4 1,1 1,5 5,5 5,1 4 2,4 2,2 4,2 4,4 4 3,2 2,2 2,4 3,4 4 3,4 4,4 4,2 3,2 8 1,5 1,1 3,1 3,2 2,2 2,4 3,4 3,5 8 5,5 5,1 3,1 3,2 4,2 4,4 3,4 3,5 8 5,5 3,5 3,4 2,4 2,2 3,2 3,1 5,1 8 1,5 3,5 3,4 4,4 4,2 3,2 3,1 1,1 3 1,5 1,1 2,3 3 4,3 5,5 5,1 2 7 1,1 1,5 4,5 2,4 4,3 2,2 4,1 7 6,5 5,5 3,4 5,3 3,2 5,1 6,1 3 3 0,0 100,100 50,49 3 1,1 2,0 1,0 3 2,1 3,2 3,1 3 5 1,2 2,5 3,5 5,2 4,1 5 7,2 8,3 7,5 10,5 10,2 4 0,0 0,75 80,5 99,1 2 4 4,2 3,3 4,5 5,3 4 7,3 5,0 2,3 4,6 2 4 4,2 3,3 4,5 5,3 4 4,6 2,3 5,0 7,3 2 4 5,3 4,5 3,3 4,2 4 4,6 2,3 5,0 7,3 2 4 5,3 4,5 3,3 4,2 4 7,3 5,0 2,3 4,6 2 20 0,0 9,0 9,11 0,11 0,2 7,2 7,9 2,9 2,4 5,4 5,5 3,5 3,8 6,8 6,3 1,3 1,10 8,10 8,1 0,1 4 4,6 4,7 5,7 5,6 View Codeans:
Data Set #1 no collisions Data Set #2 1 2 1 4 2 4 3 4 Data Set #3 no collisions Data Set #4 1 2 Data Set #5 no collisions Data Set #6 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 3 4 3 5 3 6 3 7 3 8 3 9 4 5 4 6 4 7 4 8 4 10 5 6 5 7 5 8 5 9 6 7 6 8 6 10 7 8 7 9 7 10 8 9 8 10 Data Set #7 no collisions Data Set #8 1 2 Data Set #9 1 3 2 3 Data Set #10 1 2 Data Set #11 1 2 Data Set #12 1 2 Data Set #13 1 2 Data Set #14 no collisions View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/hanbinggan/p/4251164.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的poj 3082多边形相交 'Roid Rage的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 下载大文件 不经过php 直接让apac
- 下一篇: Git_git的诞生