惭愧,自己看着都头晕,权当对过去走歪路的纪念!
/**
* 挑出传入的所有点中重复的点~
*/
-(vector<b2Vec2>*)selectDuplicatedVertices2:(vector<vector<b2Vec2>*>*)allVertices {
vector<b2Vec2> *target = new vector<b2Vec2>();
for(uint i = 0; i < allVertices->size(); i ++) {
vector<b2Vec2> *item = allVertices->at(i);
int itemSize = item->size();
// 1。初始化权值~
int *weight = new int[itemSize];
for(int j = 0; j < itemSize; ++ j) {
weight[j] =0;
}
// 2。统计权值~
int expectLineCount = 0;
for(uint j = i + 1; j < allVertices->size(); ++ j) {
vector<b2Vec2> *beCompared = allVertices->at(j);
int duplicatedVerticesCount = 0;
for(int k = 0; k < itemSize; ++ k) {
b2Vec2 m = item->at(k);
for(uint l = 0; l < beCompared->size(); l ++) {
b2Vec2 n = beCompared->at(l);
if((m.x == n.x && m.y == n.y) ||
(m.x <= n.x+_gCfg.floatErrorRange && m.x >= n.x-_gCfg.floatErrorRange &&
m.y <= n.y+_gCfg.floatErrorRange && m.y >= n.y-_gCfg.floatErrorRange)) {
weight[k] +=1;
duplicatedVerticesCount +=1;
}
}
}
// NSLog(@"duplicatedVerticesCount=%d", duplicatedVerticesCount);
/**
* duplicatedVerticesCount如果等于 1 的话表示当前2多边形只有1个顶点相交
*这两个多边形是不具有相邻边的
*只有当 duplicatedVerticesCount 等于 2 的时候才表示2多边形有1条相邻边~
*/
if(duplicatedVerticesCount == 2) {
expectLineCount +=1;
}
}
// 3。根据权值数据进行重复边的筛选~
int offset = 0;
vector<b2Vec2> candidates;
LoopLabel:
int *weightCopy = new int[itemSize];
int actualLineCount = 0;
for(int j = 0; j < itemSize; ++ j) {
weightCopy[j] = weight[j];
}
for(int j = 0; j < itemSize; ++ j) {
if(weightCopy[(j+offset)%itemSize] >= 1 && weightCopy[(j+offset+1)%itemSize] >=1) {
weightCopy[(j+offset)%itemSize] -=1;
weightCopy[(j+offset+1)%itemSize] -=1;
candidates.push_back(item->at((j+offset)%itemSize));
candidates.push_back(item->at((j+offset+1)%itemSize));
actualLineCount +=1;
}
}
delete weightCopy;
/**
*开始是这么写的判断:if(actualLineCount != expectLineCount),但是我发现程序很容易就被卡住
*然后我改成下面这样:if(actualLineCount < expectLineCount)
*因为:
* expectLineCount可能小于 actualLineCount,这个最多体现为冰块儿内部出现黑线~
*但若要使 actualLineCount 一直都小于 expectLineCount 的话,将陷入死循环致使程序卡壳~
*/
if(actualLineCount < expectLineCount) {
// NSLog(@"checkValid == NO!!");
candidates.clear();
offset +=1;
goto LoopLabel;
}
for(uint j = 0; j < candidates.size(); ++ j) {
target->push_back(candidates.at(j));
}
candidates.clear();
delete weight;
}
return target;
}