2014-02-20 83 views
2

你好我知道有一些類似的問題,但我一直無法解決我的問題。 我需要在笛卡爾座標上生成一個球體上唯一的一組點,即從球面到笛卡爾的轉換。當我這樣做時,我將這些點存儲在向量中。然而,一些重複被創建並刪除它們我嘗試過使用排序擦除和獨特的功能。問題是這種排序看不到排序我的整個向量,我不明白爲什麼?它可以很好地處理向量,我只是將數字推回,而不是由我的笛卡爾函數生成的向量向量。我知道這很簡單,我已經堅持了3天,我確信它正在凝視我的臉!代碼和輸出均低於排序和刪除重複從載體<矢量< double>>

#include <iostream> 
#include <math.h> 
#include <algorithm> 
#include <vector> 
#include <stdio.h> 

int main(int argc, const char * argv[]){ 
    std::vector<double> locations; //center of the bubble 
    locations.push_back(1.0); 
    locations.push_back(1.0); 
    locations.push_back(1.0); 
    std::vector<std::vector<double> > points; //set of points to be created around the bubble 
    double PI=atan(1)*4; 

for(int dr=1; dr<2; dr++){ 
    for (int phi=0; phi<180; phi+=90){ 
     for (int theta=0; theta<360; theta+=90){ 

      std::vector<double> row; 
      double x=locations[0]+(dr*sin(theta*(PI/180))*cos(phi*(PI/180))); 
      double y=locations[1]+(dr*cos(theta*(PI/180))); 
      double z=locations[2]+(dr*sin(theta*(PI/180))*sin(phi*(PI/180))); 
      row.push_back(x); 
      row.push_back(y); 
      row.push_back(z); 

      points.push_back(row); 


     } 
    } 
} 



std::sort(points.begin(), points.end()); //sort points 
std::cout<<"sorted points \n"; 
for (int i =0; i<points.size(); i++){ 
    std::cout<<points[i][0]<<" "<<points[i][1]<<" "<<points[i][2]<<"\n"; 
} 

points.erase(std::unique(points.begin(), points.end()), points.end()); //erase duplicates 

std::cout<<"duplicates removed \n"; 
for (int i =0; i<points.size(); i++){ 
    std::cout<< points[i][0]<<" "<<points[i][1]<<" "<<points[i][2]<<"\n"; 
} 

}

輸出

sorted points 
0 1 1 
1 1 0 
1 0 1   THIS HASN'T BEEN SORTED CORRECTLY 
1 1 2 
1 2 1 
1 2 1 
1 0 1   THIS HASN'T BEEN SORTED CORRECTLY 
2 1 1 
duplicates removed 
0 1 1 
1 1 0 
1 0 1 
1 1 2 
1 2 1 
1 0 1 
2 1 1 
+2

是的,浮點比較是一種痛苦。 – chris

+1

'for(int dr = 1; dr <2; dr ++)'你不需要循環。你只需要'const int dr = 1;'。 – timrau

+0

我會建議做一個類/結構封裝點的方面。然後你可以重載操作符來進行排序和比較。這會讓你更容易看到你正在嘗試做什麼。 –

回答

2

如果更改行:

std::cout << points[i][0] << " " << points[i][1] << " " << points[i][2] << "\n"; 

與(你還需要包括<limits><iomanip>):

std::cout << std::fixed << std::setprecision(std::numeric_limits<double>::digits10+2) << points[i][0] << " " << points[i][1] << " " << points[i][2] << "\n"; 

你會看到輸出是:

sorted points 
0.00000000000000000 0.99999999999999978 1.00000000000000000 
0.99999999999999989 0.99999999999999978 0.00000000000000000 
1.00000000000000000 0.00000000000000000 1.00000000000000022 
1.00000000000000000 1.00000000000000000 2.00000000000000000 
1.00000000000000000 2.00000000000000000 1.00000000000000000 
1.00000000000000000 2.00000000000000000 1.00000000000000000 
1.00000000000000022 0.00000000000000000 1.00000000000000000 
2.00000000000000000 1.00000000000000000 1.00000000000000000 
duplicates removed 
0.00000000000000000 0.99999999999999978 1.00000000000000000 
0.99999999999999989 0.99999999999999978 0.00000000000000000 
1.00000000000000000 0.00000000000000000 1.00000000000000022 
1.00000000000000000 1.00000000000000000 2.00000000000000000 
1.00000000000000000 2.00000000000000000 1.00000000000000000 
1.00000000000000022 0.00000000000000000 1.00000000000000000 
2.00000000000000000 1.00000000000000000 1.00000000000000000 

所以考慮近似載體已正確排序。

PS您可以在呼叫std::sort()調用中使用自定義比較器,在std::unique調用中使用自定義二進制謂詞。

+0

那麼,你認爲這個問題會被修正嗎,例如,用'int'替換'double'? – Drop

+0

@Drop是的,我認爲是這樣(至少在具體的例子中)。 – manlio

+0

哦,我明白了!我有一種感覺,這與PI的近似有關。好吧,所以我不能更改爲'int',因爲我需要每隔10度的實際代碼,我會按照您的建議嘗試,非常感謝! – user3333325

相關問題