2013-08-21 20 views
7

我有一組(X,Y)座標,它將單位正方形分割成子矩形。假設我的座標 -將類似框組合起來

  ( x1, y1) ( x2, y2)  

     (0.0000,0.0000) (0.3412,0.4175) 
     (0.7445,0.0000) (1.0000,0.6553) 
     (0.7445,0.6553) (1.0000,1.0000) 
     (0.0000,0.6553) (0.7445,1.0000) 
     (0.3412,0.0000) (0.7445,0.4175) 
     (0.3412,0.4175) (0.7445,0.6553) 
     (0.0000,0.4175) (0.3412,0.6553)....etc (total 10,000 coordinates) 

正如我只花了16組數據和這些座標的一個例子拆分我像方盒子類似的這 -

enter image description here

定義

那些具有相似數量鄰居的框被認爲是類似的框。對於上面的圖片框[8],框[13]等有最近的鄰居。所以他們被認爲是相似的盒子。

下面的圖像應該使這個clear-

enter image description here

::我的問題::

從圖片我們可以見式

對於盒[8 ]最近的框是:

盒(1)(其具有4個鄰居)

盒[4](其中也有4個鄰居)

箱[14](具有4個鄰居)

框[16](有4個鄰居)

所以在這種情況下,最近的盒子的鄰居的總和= 4 + 4 + 4 + 4 = 16

再次箱[13]最近的框是:

盒[3](其具有6個鄰居)

盒[5](其中也有4個相鄰)

箱〔6〕(具有3個鄰居)

盒[12](具有3個鄰居)

因此,在這種情況下,最接近盒鄰居的總和= 6 + 4 + 3 + 3 = 16

在這裏總鄰居的(相似的框)框[8]和框[13] = 16 + 16 = 32。

同樣我想把所有有4個鄰居的盒子組合起來,找到他們最近盒子的鄰居的總和。並繼續爲每個類似的組。

我的代碼

這裏是我的代碼。

#include <iostream> 
#include <cstdlib> 
#include <vector> 
#include <stdio.h> 

using namespace std; 

class Rect { 
public: 
double x1, x2, y1, y2; // coordinates 

Rect(double X1, double Y1, double X2, double Y2) { 
    if (X1 < X2) { 
    x1 = X1; x2 = X2; 
    } else { 
    x2 = X1; x1 = X2; 
    } 
    if (Y1 < Y2) { 
    y1 = Y1; y2 = Y2; 
    } else { 
    y2 = Y1; y1 = Y2; 
    } 


} 

bool isAdjacent(Rect rect) { 
    if (x1 == rect.x1 || x1 == rect.x2 || 
     x2 == rect.x1 || x2 == rect.x2) { 
     // use only < when comparing y1 and rect.y2 avoids sharing only a corner 
     if (y1 >= rect.y1 && y1 < rect.y2) { 
     return true; 
     } 
     if (y2 > rect.y1 && y2 <= rect.y2) { 
     return true; 
     } 
     if (rect.y1 >= y1 && rect.y1 < y2) { 
     return true; 
     } 
     if (rect.y2 > y1 && rect.y2 <= y2) { 
     return true; 
     } 
    } 
    if (y1 == rect.y1 || y1 == rect.y2 || 
     y2 == rect.y1 || y2 == rect.y2) { 
     if (x1 >= rect.x1 && x1 < rect.x2) { 
     return true; 
     } 
     if (x2 > rect.x1 && x2 <= rect.x2) { 
     return true; 
     } 
     if (rect.x1 >= x1 && rect.x1 < x2) { 
     return true; 
     } 
     if (rect.x2 > x1 && rect.x2 <= x2) { 
     return true; 
     } 
    } 
    return false; 
    } 

}; 



void isNearest(int b){ 

vector<Rect> rects;  
       //Rect( x1 , y1 , x2 , y2 ) 
    rects.push_back(Rect(0.0000,0.0000, 0.8147,0.1355)); 
    rects.push_back(Rect(0.8147,0.0000, 1.0000,0.1355)); 

    rects.push_back(Rect(0.8147,0.1355, 0.9058,0.8350)); 
    rects.push_back(Rect(0.0000,0.1355, 0.1270,0.9689)); 

    rects.push_back(Rect(0.9058,0.1355, 0.9134,0.2210)); 
    rects.push_back(Rect(0.9058,0.8350, 1.0000,1.0000)); 
    rects.push_back(Rect(0.8147,0.8350, 0.9058,1.0000)); 


    rects.push_back(Rect(0.1270,0.1355, 0.6324,0.3082)); 
    rects.push_back(Rect(0.1270,0.9689, 0.8147,1.0000)); 
    rects.push_back(Rect(0.0000,0.9689, 0.1270,1.0000)); 

    rects.push_back(Rect(0.9134,0.1355, 1.0000,0.2210)); 
    rects.push_back(Rect(0.9134,0.2210, 1.0000,0.8350)); 
    rects.push_back(Rect(0.9058,0.2210, 0.9134,0.8350)); 


    rects.push_back(Rect(0.6324,0.1355, 0.8147,0.3082)); 
    rects.push_back(Rect(0.6324,0.3082, 0.8147,0.9689)); 
    rects.push_back(Rect(0.1270,0.3082, 0.6324,0.9689)); 


    int nearBox_count = 0; 

    double TotalArea=0; 


    for (int x = 0; x < rects.size(); ++x) { 

    if (rects[b].isAdjacent(rects[x])) { 


     if (x==b) { 
continue; //this is our box , so do not count it. 
} 


nearBox_count++; 

printf("box[%d] is nearest to box[%d] \n", (b+1), (x+1)); 

} 
} 

printf("Total number of nearest box for [%d] is %d \n",(b+1),nearBox_count); 
printf("\n"); 

} 


int main() { 

    for (int i = 0; i < 16; ++i) 
    { 
    isNearest(i); 
    } 

return 0; 
} 

它給出正確的結果就像這個 -

box[1] is nearest to box[2] 
box[1] is nearest to box[4] 
box[1] is nearest to box[8] 
box[1] is nearest to box[14] 
Total number of nearest box for [1] is 4 

box[2] is nearest to box[1] 
box[2] is nearest to box[3] 
box[2] is nearest to box[5] 
box[2] is nearest to box[11] 
Total number of nearest box for [2] is 4 

box[3] is nearest to box[2] 
box[3] is nearest to box[5] 
box[3] is nearest to box[7] 
box[3] is nearest to box[13] 
box[3] is nearest to box[14] 
box[3] is nearest to box[15] 
Total number of nearest box for [3] is 6 

box[4] is nearest to box[1] 
box[4] is nearest to box[8] 
box[4] is nearest to box[10] 
box[4] is nearest to box[16] 
Total number of nearest box for [4] is 4 

box[5] is nearest to box[2] 
box[5] is nearest to box[3] 
box[5] is nearest to box[11] 
box[5] is nearest to box[13] 
Total number of nearest box for [5] is 4 

box[6] is nearest to box[7] 
box[6] is nearest to box[12] 
box[6] is nearest to box[13] 
Total number of nearest box for [6] is 3 

box[7] is nearest to box[3] 
box[7] is nearest to box[6] 
box[7] is nearest to box[9] 
box[7] is nearest to box[15] 
Total number of nearest box for [7] is 4 

box[8] is nearest to box[1] 
box[8] is nearest to box[4] 
box[8] is nearest to box[14] 
box[8] is nearest to box[16] 
Total number of nearest box for [8] is 4 

box[9] is nearest to box[7] 
box[9] is nearest to box[10] 
box[9] is nearest to box[15] 
box[9] is nearest to box[16] 
Total number of nearest box for [9] is 4 

box[10] is nearest to box[4] 
box[10] is nearest to box[9] 
Total number of nearest box for [10] is 2 

box[11] is nearest to box[2] 
box[11] is nearest to box[5] 
box[11] is nearest to box[12] 
Total number of nearest box for [11] is 3 

box[12] is nearest to box[6] 
box[12] is nearest to box[11] 
box[12] is nearest to box[13] 
Total number of nearest box for [12] is 3 

box[13] is nearest to box[3] 
box[13] is nearest to box[5] 
box[13] is nearest to box[6] 
box[13] is nearest to box[12] 
Total number of nearest box for [13] is 4 

box[14] is nearest to box[1] 
box[14] is nearest to box[3] 
box[14] is nearest to box[8] 
box[14] is nearest to box[15] 
Total number of nearest box for [14] is 4 

box[15] is nearest to box[3] 
box[15] is nearest to box[7] 
box[15] is nearest to box[9] 
box[15] is nearest to box[14] 
box[15] is nearest to box[16] 
Total number of nearest box for [15] is 5 

box[16] is nearest to box[4] 
box[16] is nearest to box[8] 
box[16] is nearest to box[9] 
box[16] is nearest to box[15] 
Total number of nearest box for [16] is 4 

雖然它可以識別最近箱和計數的鄰居的數目,但我無法弄清楚哪能組類似的箱子(如如上所述)並找出總和。

而我卡在這裏。誰能幫我?

更新程式碼

vector<CheckRect> rects; 

unsigned isNearest(unsigned b, vector<unsigned>& neighbours) { 

    unsigned nearBox_count = 0; 

    for (unsigned x = 0; x < rects.size(); ++x) { 
    if (rects[b].isAdjacent(rects[x])) { 
     if (x==b) continue; //this is our box , so do not count it. 
     nearBox_count++; 
     printf("box[%d] is nearest to box[%d] \n", (b+1), (x+1)); 
     neighbours.push_back(x); 
    } 
    } 

    printf("Total number of nearest box for [%d] is %d \n", 
     (b+1), nearBox_count); 
    printf("\n"); 

    return nearBox_count; 
} 

int main(){ 

cin>>N; 

for(int b=0; b<N; b++){ 

    ifstream inputFile1("RectCoordinates.txt"); //input from the file previously generated 
    int rect_number; 
    double xa0,ya0,xa1,ya1; 
    int neighbours; 
    isNearest(b, &neighbours);// This is the line that causing my ERROR 


    } 
vector<unsigned> nearBox_count(rects.size()); 
    vector< vector<unsigned> > neighbours(rects.size()); 
    for (unsigned i = 0; i < rects.size(); ++i) { 
    nearBox_count[i] = isNearest(i, neighbours[i]); 
    } 

    // Calculate the sums of neighbouring boxes 
    vector<unsigned> neighCount(rects.size(), 0); 
    for (unsigned i = 0; i < rects.size(); i++) { 
    for (unsigned j = 0; j < neighbours[i].size(); j++) { 
     neighCount[i] += nearBox_count[neighbours[i][j]]; 
    } 
    } 

    // Calculate your result 
    map<unsigned,unsigned> finalCount; 
    for (unsigned i = 0; i < rects.size(); i++) 
    { 
    if (finalCount.count(nearBox_count[i]) == 0) 
     finalCount[nearBox_count[i]] = neighCount[i]; 
    else 
     finalCount[nearBox_count[i]] += neighCount[i]; 
    } 

    // Print the result 
    for (map<unsigned,unsigned>::iterator it = finalCount.begin(); 
     it != finalCount.end(); ++it) { 
    printf("Sum neighbours for the neighbours of similar boxes with %d " 
      "neighbours is %d\n", it->first, it->second); 
    } 

    return 0; 
} 

給我的錯誤 -

ss.cpp: In function ‘int main()’: 
ss.cpp:102:29: error: invalid initialization of reference of type ‘std::vector<unsigned int>&’ from expression of type ‘unsigned int’ 
ss.cpp:22:10: error: in passing argument 2 of ‘unsigned int isNearest(unsigned int, std::vector<unsigned int>&)’ 

我怎樣才能解決呢?

+0

@haccks是的,我是一名物理學的學生。處理這類問題一段時間。這是我堅持的問題的一部分。 :( – aries0152

+0

我見過很多** Box **問題,這就是爲什麼我問你 – haccks

+0

@haccks你說得對,我希望這可能是最後一個問題,但這一切都取決於結果。 – aries0152

回答

0

除了嘗試計算您的值,我還對代碼進行了一些小修改。

由於您的所有列表索引都不是負數,並且很可能未來將有大量矩形,因此我建議您將所有int s設置爲unsigned。這有一個額外的好處,就是可以抑制某些編譯器警告,比較下面代碼中的有符號和無符號整數。

我建議您做的第二個更改是僅聲明rects而不是每次迭代isNearest一次。在下面的代碼中,我通過使rects成爲一個全局變量並創建一個獨立的函數來初始化它來實現這一目標。通過使rects成爲一個全局變量,您現在可以用rects.size()替換所有16(減少在添加完整數據集時忘記更改一個16的機會)。

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <vector> 
#include <map> 
#include <stdio.h> 

using namespace std; 

class Rect { 
public: 
    double x1, x2, y1, y2; // coordinates 

    Rect(double X1, double Y1, double X2, double Y2) { 
    if (X1 < X2) { 
     x1 = X1; x2 = X2; 
    } else { 
     x2 = X1; x1 = X2; 
    } 
    if (Y1 < Y2) { 
     y1 = Y1; y2 = Y2; 
    } else { 
     y2 = Y1; y1 = Y2; 
    } 
} 

bool isAdjacent(Rect rect) { 
    if (x1 == rect.x1 || x1 == rect.x2 || 
     x2 == rect.x1 || x2 == rect.x2) { 
     // use only < when comparing y1 and rect.y2 avoids sharing only a corner 
     if (y1 >= rect.y1 && y1 < rect.y2) { 
     return true; 
     } 
     if (y2 > rect.y1 && y2 <= rect.y2) { 
     return true; 
     } 
     if (rect.y1 >= y1 && rect.y1 < y2) { 
     return true; 
     } 
     if (rect.y2 > y1 && rect.y2 <= y2) { 
     return true; 
     } 
    } 
    if (y1 == rect.y1 || y1 == rect.y2 || 
     y2 == rect.y1 || y2 == rect.y2) { 
     if (x1 >= rect.x1 && x1 < rect.x2) { 
     return true; 
     } 
     if (x2 > rect.x1 && x2 <= rect.x2) { 
     return true; 
     } 
     if (rect.x1 >= x1 && rect.x1 < x2) { 
     return true; 
     } 
     if (rect.x2 > x1 && rect.x2 <= x2) { 
     return true; 
     } 
    } 
    return false; 
    } 
}; 

vector<Rect> rects; 

unsigned isNearest(unsigned b, vector<unsigned>& neighbours) { 

    unsigned nearBox_count = 0; 

    for (unsigned x = 0; x < rects.size(); ++x) { 
    if (rects[b].isAdjacent(rects[x])) { 
     if (x==b) continue; //this is our box , so do not count it. 
     nearBox_count++; 
     printf("box[%d] is nearest to box[%d] \n", (b+1), (x+1)); 
     neighbours.push_back(x); 
    } 
    } 

    printf("Total number of nearest box for [%d] is %d \n", 
     (b+1), nearBox_count); 
    printf("\n"); 

    return nearBox_count; 
} 

void initRects(void) { 

       //Rect( x1 , y1 , x2 , y2 ) 
    rects.push_back(Rect(0.0000,0.0000, 0.8147,0.1355)); 
    rects.push_back(Rect(0.8147,0.0000, 1.0000,0.1355)); 

    rects.push_back(Rect(0.8147,0.1355, 0.9058,0.8350)); 
    rects.push_back(Rect(0.0000,0.1355, 0.1270,0.9689)); 

    rects.push_back(Rect(0.9058,0.1355, 0.9134,0.2210)); 
    rects.push_back(Rect(0.9058,0.8350, 1.0000,1.0000)); 
    rects.push_back(Rect(0.8147,0.8350, 0.9058,1.0000)); 


    rects.push_back(Rect(0.1270,0.1355, 0.6324,0.3082)); 
    rects.push_back(Rect(0.1270,0.9689, 0.8147,1.0000)); 
    rects.push_back(Rect(0.0000,0.9689, 0.1270,1.0000)); 

    rects.push_back(Rect(0.9134,0.1355, 1.0000,0.2210)); 
    rects.push_back(Rect(0.9134,0.2210, 1.0000,0.8350)); 
    rects.push_back(Rect(0.9058,0.2210, 0.9134,0.8350)); 


    rects.push_back(Rect(0.6324,0.1355, 0.8147,0.3082)); 
    rects.push_back(Rect(0.6324,0.3082, 0.8147,0.9689)); 
    rects.push_back(Rect(0.1270,0.3082, 0.6324,0.9689)); 
} 

void readRects(const string& filename) { 

    ifstream fpInput(filename.c_str()); 
    double dTemp[4]; 

    while (true) { 
    for (unsigned i = 0; i < 4; i++) fpInput >> dTemp[i]; 
    if (!fpInput.good()) break; 
    rects.push_back(Rect(dTemp[0], dTemp[1], dTemp[2], dTemp[3])); 
    } 

    fpInput.close(); 
} 

int main() { 

    // Initialize the vector rects 
    //initRects(); 
    readRects("RectCoordinates.txt"); 

    vector<unsigned> nearBox_count(rects.size()); 
    vector< vector<unsigned> > neighbours(rects.size()); 
    for (unsigned i = 0; i < rects.size(); ++i) { 
    nearBox_count[i] = isNearest(i, neighbours[i]); 
    } 

    // Calculate the sums of neighbouring boxes 
    vector<unsigned> neighCount(rects.size(), 0); 
    for (unsigned i = 0; i < rects.size(); i++) { 
    for (unsigned j = 0; j < neighbours[i].size(); j++) { 
     neighCount[i] += nearBox_count[neighbours[i][j]]; 
    } 
    } 

    // Calculate your result 
    map<unsigned,unsigned> finalCount; 
    for (unsigned i = 0; i < rects.size(); i++) 
    { 
    if (finalCount.count(nearBox_count[i]) == 0) { 
     finalCount[nearBox_count[i]] = neighCount[i]; 
    } else { 
     finalCount[nearBox_count[i]] += neighCount[i]; 
    } 
    } 

    // Print the result 
    for (map<unsigned,unsigned>::iterator it = finalCount.begin(); 
     it != finalCount.end(); ++it) { 
    printf("Sum neighbours for the neighbours of similar boxes with %d " 
      "neighbours is %d\n", it->first, it->second); 
    } 

    return 0; 
} 

更新:上面的代碼現在可以通過任一指定Rect S上的源文件的內部或從外部文件裝入一起使用。在上面的變形例,輸入文件是RectCoordinates.txt

0.0000 0.0000 0.8147 0.1355 
0.8147 0.0000 1.0000 0.1355 

0.8147 0.1355 0.9058 0.8350 
0.0000 0.1355 0.1270 0.9689 

0.9058 0.1355 0.9134 0.2210 
0.9058 0.8350 1.0000 1.0000 
0.8147 0.8350 0.9058 1.0000 


0.1270 0.1355 0.6324 0.3082 
0.1270 0.9689 0.8147 1.0000 
0.0000 0.9689 0.1270 1.0000 

0.9134 0.1355 1.0000 0.2210 
0.9134 0.2210 1.0000 0.8350 
0.9058 0.2210 0.9134 0.8350 


0.6324 0.1355 0.8147 0.3082 
0.6324 0.3082 0.8147 0.9689 
0.1270 0.3082 0.6324 0.9689 

以上輸出結果:

Sum neighbours for the neighbours of similar boxes with 2 neighbours is 8 
Sum neighbours for the neighbours of similar boxes with 3 neighbours is 32 
Sum neighbours for the neighbours of similar boxes with 4 neighbours is 165 
Sum neighbours for the neighbours of similar boxes with 5 neighbours is 22 
Sum neighbours for the neighbours of similar boxes with 6 neighbours is 25 
+0

不錯!我正在考慮使用一個文本文件,並從rects的值中獲取輸入。直到現在,它似乎像我想要的那樣工作。讓我檢查其他值。 – aries0152

+0

你的代碼給了我正確的結果。但是我從文本文件中獲取rects的輸入時遇到了問題。它顯示了類型'無符號整型''_'的表達式'std :: vector &'引用的無效初始化錯誤。我該如何解決它?我**已更新**我的代碼。請檢查。 – aries0152

+0

@ aries0152我已更新我的帖子。代碼現在讀取包含數據點的文件。錯誤是因爲你試圖將'int'而不是'vector '傳遞給更新的函數'isNearest'。 'isNearest'返回鄰居的數量並將實際的鄰居存儲在變量'neighbours'中。 – ilent2

0

那麼,我想你可以使用向量,但這種方式可能需要大量的內存空間。 說一下,做矢量4neighbours - 一見鍾情,我會說每個盒子至少有4個鄰居(但是你可以這麼做,但是要爲每個可能的鄰居都這樣做 - 希望它可以被計算出來,而且它不會超大)。然後,在檢查每個矩形的鄰居數量時,在相應的矢量上使用回推。

printf("Total number of nearest box for [%d] is %d \n",(b+1),nearBox_count); 
if(nearBox_count == 4) 
4neighbours.pushback(rects[b]); 
else if (nearBox_count == 5) 
5neighbours.pushback(rects[b]); 
else if -etc- 

然後,經過矢量和矢量的每個成員,檢查鄰居,併爲每個鄰居計算鄰居和概括起來(你會築巢兩個環路 - 一個環路一個循環內)

例子:不編碼

int TotalNeighbours = 0; 
while(go through all the boxes in the vector) 
{ 
    while(go through all the neighbours of each box) 
    { 
    TotalNeighbours++; 
    } 
} 

我相信這樣的事情可能會奏效,有,但上面提到的侷限性。 取決於最大鄰居數量:

  • 大量的內存使用情況;
  • 大量的書面代碼(很多if語句);

編輯:一個盒子可以有0個鄰居,一個休息至少1鄰居,而不是4

+0

我已經想過了但這樣我必須使用大約40-50 if語句 – aries0152

+0

@ aries0152是的,如果我沒有弄錯,每個可能數量的鄰居都有一個if語句,它確實是最後一個語句或類似的東西。如果沒有更好的/可以理解的解決方案出現,並且您有時間,很多代碼都可以實現。但是,在這種情況下,我相信會有更好的方式出現。 – Sampaio

+0

@Sampaio雖然沒有在上面給出的例子中,邊框可以有1個鄰居,如果只有一個框0鄰居。 – ilent2

3

甚則試圖保持在一定的數據結構矩形之間的關係,這可能是一個更好的想法是讓矩形對象本身變得聰明,並意識到它的鄰居數量和他們是誰。

例如(不完全proptotype來說明想法):

class Rect { 
public: 
//methods 
Rect(double X1, double Y1, double X2, double Y2); 

//const access 
double getX1() const; 
double getX2() const; 
double getY1() const; 
double getY2() const; 

int numNeighbors() const { return neighbors.size();} 
int sumOfNeighbors() const { int res(0); for(size_t i=0;i< neighbors.size();++i) res += neighbors[i]->numNeighbors(); return res;} 
std::vector<Rect*> getNeighbors() {return neighbors}; 

void addNeighbor(Rect* newNeighbor) {neighbors.push_back(newNeighbor);} 

//data 
private: 

double x1, x2, y1, y2; // coordinates 
std::vector<Rect*> neighbors; 
}; 

有了這樣的rect類,你可以添加鄰居到每個矩形,找回自己的每一個矩形的所有鄰國,和所有他們的鄰居 - 所有關係被保留在rect本身而不是一些外部對象中,主程序的代碼應該是非常小的。

一旦你填充了rects,你可以簡單地迭代它們,挑選具有所需數量的鄰居,並對它們進行任何操作。

1

我想,如果你想徹底簡化了這一切,你可以使用Kobelevskiy先生的建議:

#include <iostream> 
#include <cstdlib> 
#include <vector> 
#include <stdio.h> 

using namespace std; 

class Rect { 
public: 
double x1, x2, y1, y2; // coordinates 

//methods 
Rect(double X1, double Y1, double X2, double Y2) { 
    if (X1 < X2) { 
    x1 = X1; x2 = X2; 
    } else { 
    x2 = X1; x1 = X2; 
    } 
    if (Y1 < Y2) { 
    y1 = Y1; y2 = Y2; 
    } else { 
    y2 = Y1; y1 = Y2; 
    } 
} 

~Rect() 
{ 
}; 

int numNeighbors() const { return neighbors.size();} 
int sumOfNeighbors() const { int res(0); for(size_t i=0;i< neighbors.size();++i) res += neighbors[i]->numNeighbors(); return res;} 
std::vector<Rect*> getNeighbors() {return neighbors;}; 

void addNeighbor(Rect* newNeighbor) {neighbors.push_back(newNeighbor);} 

//data 
std::vector<Rect*> neighbors; 

bool isAdjacent(Rect* rect) { 
    if (x1 == rect->x1 || x1 == rect->x2 || 
     x2 == rect->x1 || x2 == rect->x2) { 
     // use only < when comparing y1 and rect->y2 avoids sharing only a corner 
     if (y1 >= rect->y1 && y1 < rect->y2) { 
     return true; 
     } 
     if (y2 > rect->y1 && y2 <= rect->y2) { 
     return true; 
     } 
     if (rect->y1 >= y1 && rect->y1 < y2) { 
     return true; 
     } 
     if (rect->y2 > y1 && rect->y2 <= y2) { 
     return true; 
     } 
    } 
    if (y1 == rect->y1 || y1 == rect->y2 || 
     y2 == rect->y1 || y2 == rect->y2) { 
     if (x1 >= rect->x1 && x1 < rect->x2) { 
     return true; 
     } 
     if (x2 > rect->x1 && x2 <= rect->x2) { 
     return true; 
     } 
     if (rect->x1 >= x1 && rect->x1 < x2) { 
     return true; 
     } 
     if (rect->x2 > x1 && rect->x2 <= x2) { 
     return true; 
     } 
    } 
    return false; 
    } 

}; 

vector<Rect*> rects; 

void CalculateAdjacentsForRect(unsigned int rects_element){ 

    for (unsigned int x = 0; x < rects.size(); x++) { 
     if (rects[rects_element]->isAdjacent(rects[x])) { 
      if (x==rects_element) { 
       continue; //this is our box , so do not count it. 
      } 
      rects[rects_element]->addNeighbor(rects[x]); 
     } 
    } 
} 

const int MAX_ADJACENT_RECTS = 10; 

int main() { 

        //Rect( x1 , y1 , x2 , y2 ) 
    rects.push_back(&Rect(0.0000,0.0000, 0.8147,0.1355)); 
    rects.push_back(&Rect(0.8147,0.0000, 1.0000,0.1355)); 

    rects.push_back(&Rect(0.8147,0.1355, 0.9058,0.8350)); 
    rects.push_back(&Rect(0.0000,0.1355, 0.1270,0.9689)); 

    rects.push_back(&Rect(0.9058,0.1355, 0.9134,0.2210)); 
    rects.push_back(&Rect(0.9058,0.8350, 1.0000,1.0000)); 
    rects.push_back(&Rect(0.8147,0.8350, 0.9058,1.0000)); 


    rects.push_back(&Rect(0.1270,0.1355, 0.6324,0.3082)); 
    rects.push_back(&Rect(0.1270,0.9689, 0.8147,1.0000)); 
    rects.push_back(&Rect(0.0000,0.9689, 0.1270,1.0000)); 

    rects.push_back(&Rect(0.9134,0.1355, 1.0000,0.2210)); 
    rects.push_back(&Rect(0.9134,0.2210, 1.0000,0.8350)); 
    rects.push_back(&Rect(0.9058,0.2210, 0.9134,0.8350)); 


    rects.push_back(&Rect(0.6324,0.1355, 0.8147,0.3082)); 
    rects.push_back(&Rect(0.6324,0.3082, 0.8147,0.9689)); 
    rects.push_back(&Rect(0.1270,0.3082, 0.6324,0.9689)); 

    for (unsigned int i = 0; i < rects.size(); i++) 
    { 
     CalculateAdjacentsForRect(i); 
    } 

    for (unsigned int i = 0; i < rects.size(); i++) 
    { 
     cout << "\nRect" << i << " has a neighbor sum of " << rects[i]->sumOfNeighbors(); 
    } 

    cout << "\n"; 

    for (int ix = 0; ix < MAX_ADJACENT_RECTS; ix++) 
    { 
     int num_rects_with_this_num_of_adjacents = 0; 
     int num_adjacents_total_for_similar_rects = 0; 
     for (unsigned int i = 0; i < rects.size(); i++) { 
      if (rects[i]->numNeighbors() == ix) { 
       num_rects_with_this_num_of_adjacents++; 
       num_adjacents_total_for_similar_rects += rects[i]->sumOfNeighbors(); 
      } 
     } 
     cout << "\nThere are " << num_rects_with_this_num_of_adjacents << " rects with " << ix << " adjacent rects. They have a cum neighbor sum of " << num_adjacents_total_for_similar_rects; 
    } 

    return 0; 
} 
+0

謝謝,我學到了一種解決這個問題的新方法。投了票。 :) – aries0152

+0

@ aries0152看到這個版本。科貝萊夫斯基先生的框架大大簡化了這一切。我想最好是使用指針來處理這些事情。當你試圖通過向量來引用和比較元素時,它肯定會有所幫助。我遇到了很多與我的非指針版本有關的問題,這些版本都與此版本無關。 – Stepan1010

+0

@Ilya Kobelevskiy看我的編輯。 – Stepan1010