2015-01-10 23 views
0

我需要一個函數讓我找到n個隨機點。這些點必須與給定點具有相同的距離。你可以幫我嗎?從給定點獲得N個隨機等距點

void createCpoints() 
{ 
    int xcenter=3; 
    int ycenter=3; 
    int radius=3; 
    double x[N]; 
    double y[N]; 
    //... 
} 
+6

提示:[polar coordinates](https://en.wikipedia.org/wiki/Polar_coordinate_system) – 5gon12eder

回答

2

正如@ 5gon12eder說,你可以用極座標與初始點作爲虛擬中點:

#include <math.h> 

//... 

for(int i = 0; i < n; i++) { 
    double alpha = 2.0d*M_PI*((double) rand()/RAND_MAX); 
    x[i] = radius*cos(alpha)+x0; 
    y[i] = radius*sin(alpha)+y0; 
} 

(x0,y0)原點的座標。

+0

應該不是cmath而不是math.h? – JorenHeit

+0

@JorenHeit:[所有函數/常量](http://www.cs.cf.ac.uk/Dave/C/node17.html)在'math.h'中定義。而'cmath'沒有定義[常量自動](http://stackoverflow.com/questions/6563810/m-pi-works-with-math-h-but-not-with-cmath-in-visual-studio )。 –

2

就產生N個角度,並計算出(X,Y),從那裏用

x1 = xCenter + r * cos(theta1) 
y1 = yCenter + r * sin(theta1) 

座標(注:這不應該是隨時可以使用C++代碼,如果你。需要的語言幫助,你需要更具體)

相關問題