inner edge消除算法

图:


核心逻辑:

    vector<b2Vec2>* allSegments = new vector<b2Vec2>();
    float multiplyFactor = PTM_RATIO * CC_CONTENT_SCALE_FACTOR();
    for(BYPolygon* polygon in comp.polyList) {
        if(!polygon.isPirate) {
            for(int k = 0; k < polygon.verticesCount; ++ k) {
                polygon.vertices[k] += cen;
                polygon.vertices[k] *= multiplyFactor;
            }
            
            glVertexPointer(2, GL_FLOAT, 0, polygon.vertices);
            glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
            glDrawArrays(GL_TRIANGLE_FAN, 0, polygon.verticesCount);
            
            for(int k = 0; k < polygon.verticesCount; ++ k) {
                b2Vec2 v1 = polygon.vertices[k];
                b2Vec2 v2 = (k + 1) < polygon.verticesCount ? polygon.vertices[k + 1] : polygon.vertices[0];
                allSegments->push_back(b2Vec2(v1.x, v1.y));
                allSegments->push_back(b2Vec2(v2.x, v2.y));
            }
        } else {
            [pirateArray addObject:polygon];
        }
    }
    
    vector<b2Vec2>* uselessSegments = new vector<b2Vec2>();
    for(uint i = 0; i < allSegments->size()/2; ++ i) {
        b2Vec2 m = allSegments->at(i * 2);
        b2Vec2 n = allSegments->at(i * 2 + 1);
        for(uint j = i + 1; j < allSegments->size()/2; ++ j) {
            b2Vec2 p = allSegments->at(j * 2);
            b2Vec2 q = allSegments->at(j * 2 + 1);
            if(m.x==q.x && m.x==q.x && m.y==q.y && m.y==q.y) {
                if(n.x==p.x && n.x== p.x && n.y==p.y && n.y==p.y) {
                    uselessSegments->push_back(b2Vec2(m.x, m.y));
                    uselessSegments->push_back(b2Vec2(n.x, n.y));
                    break;
                }
            }
        }
    }
    if(uselessSegments->size() != 0) {
        for(uint i = 0; i < uselessSegments->size()/2; ++ i) {
            b2Vec2 m = uselessSegments->at(i * 2);
            b2Vec2 n = uselessSegments->at(i * 2 + 1);
            uint token = 0;
            for(uint j = 0; j < allSegments->size()/2; ++ j) {
                b2Vec2 p = allSegments->at(j * 2);
                b2Vec2 q = allSegments->at(j * 2 + 1);
                if(m.x==p.x && m.y==p.y && n.x==q.x && n.y==q.y) {
                    token = j;
                    vector<b2Vec2>::iterator begin = allSegments->begin();
                    allSegments->erase(begin + j * 2);
                    allSegments->erase(begin + j * 2);
                    break;
                }
            }
            for(uint j = token; j < allSegments->size()/2; ++ j) {
                b2Vec2 p = allSegments->at(j * 2);
                b2Vec2 q = allSegments->at(j * 2 + 1);
                if(m.x==q.x && m.y==q.y && n.x==p.x && n.y==p.y) {
                    vector<b2Vec2>::iterator begin = allSegments->begin();
                    allSegments->erase(begin + j * 2);
                    allSegments->erase(begin + j * 2);
                    break;
                }
            }
        }
        uselessSegments->clear();
    }
    delete uselessSegments;
    
    glVertexPointer(2, GL_FLOAT, 0, &allSegments->at(0));
    glColor4f(0, 0, 0, 1);
    glDrawArrays(GL_LINES, 0, allSegments->size());
    
    allSegments->clear();
    delete allSegments;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值