我有一組(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組數據和這些座標的一個例子拆分我像方盒子類似的這 -
定義
那些具有相似數量鄰居的框被認爲是類似的框。對於上面的圖片框[8],框[13]等有最近的鄰居。所以他們被認爲是相似的盒子。
下面的圖像應該使這個clear-
::我的問題::
從圖片我們可以見式
對於盒[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>&)’
我怎樣才能解決呢?
@haccks是的,我是一名物理學的學生。處理這類問題一段時間。這是我堅持的問題的一部分。 :( – aries0152
我見過很多** Box **問題,這就是爲什麼我問你 – haccks
@haccks你說得對,我希望這可能是最後一個問題,但這一切都取決於結果。 – aries0152