简单多边形三角化(暴力)
生活随笔
收集整理的這篇文章主要介紹了
简单多边形三角化(暴力)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡單多邊形三角化(暴力)
說在前面
網上流傳著各種神奇的多邊形三角剖分算法,但是講道理,實現難度太高了。。。也沒有搜到其他人的實現。這里寫個最暴力的做法。。隨機數據驗證沒問題,歡迎 hack
實現
一個簡單多邊形的耳朵定義為:如果一個凸點與他相鄰的點構成的三角形內,沒有其他多邊形的點,那么他是一個耳朵。
我們的思想很簡單,每次切掉一個耳朵,直到多邊形變為一個三角形。
核心代碼
typedef vector<vector<P>> VPP; VPP divPtoT(vector<P> ps){ // O(n^3) 簡單多邊形三角剖分 多邊形逆時針VPP T; T.clear();if(ps.size()<3) return T;list<int> L;rep(i, 0, ps.size()-1) L.pb(i);auto ck = [&](P A, P B, P C) {if( crossOp(B,C,A) < 0 ) return false;for(int p: L) {if( ps[p] == A || ps[p] == B || ps[p] == C ) continue;if( crossOp(A,B,ps[p])>0&&crossOp(B,C,ps[p])>0&&crossOp(C,A,ps[p])>0 ) return false;}return true;};P A,B,C;while(L.size() > 3) {auto it = L.begin();A = ps[*it]; ++it; B = ps[*it]; --it; C = ps[*--L.end()];if(ck(A,B,C)) { L.erase(it); T.pb({A,B,C}); continue; }auto ed = --L.end();B = ps[*ed]; -- ed; A = ps[*ed]; ++ ed; C = ps[*L.begin()];if(ck(A,B,C)) { L.erase(ed); T.pb({A,B,C}); continue; }it = ++L.begin();for( ; it != ed; ++ it) {B = ps[*it]; --it; A = ps[*it]; ++it; ++it; C = ps[*it]; --it;if(ck(A,B,C)) { L.erase(it); T.pb({A,B,C}); break; }}}if(L.size() == 3) {vector<P> tmp; for(auto it = L.begin(); it != L.end(); ++ it) tmp.pb(ps[*it]);T.pb(tmp);}return T; }實驗
使用 cyaron 生成簡單多邊形,用三角剖分求面積,與實際多邊形面積比較判斷。
樣例:
生成:
轉載于:https://www.cnblogs.com/RRRR-wys/p/11430147.html
總結
以上是生活随笔為你收集整理的简单多边形三角化(暴力)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces 1176F
- 下一篇: Top Secret Task(dp+滚