简单多边形三角化(暴力)
生活随笔
收集整理的這篇文章主要介紹了
简单多边形三角化(暴力)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
簡(jiǎn)單多邊形三角化(暴力)
說在前面
網(wǎng)上流傳著各種神奇的多邊形三角剖分算法,但是講道理,實(shí)現(xiàn)難度太高了。。。也沒有搜到其他人的實(shí)現(xiàn)。這里寫個(gè)最暴力的做法。。隨機(jī)數(shù)據(jù)驗(yàn)證沒問題,歡迎 hack
實(shí)現(xiàn)
一個(gè)簡(jiǎn)單多邊形的耳朵定義為:如果一個(gè)凸點(diǎn)與他相鄰的點(diǎn)構(gòu)成的三角形內(nèi),沒有其他多邊形的點(diǎn),那么他是一個(gè)耳朵。
我們的思想很簡(jiǎn)單,每次切掉一個(gè)耳朵,直到多邊形變?yōu)橐粋€(gè)三角形。
核心代碼
typedef vector<vector<P>> VPP; VPP divPtoT(vector<P> ps){ // O(n^3) 簡(jiǎn)單多邊形三角剖分 多邊形逆時(shí)針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; }實(shí)驗(yàn)
使用 cyaron 生成簡(jiǎn)單多邊形,用三角剖分求面積,與實(shí)際多邊形面積比較判斷。
樣例:
生成:
轉(zhuǎn)載于:https://www.cnblogs.com/RRRR-wys/p/11430147.html
總結(jié)
以上是生活随笔為你收集整理的简单多边形三角化(暴力)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces 1176F
- 下一篇: Top Secret Task(dp+滚