2011-09-15 48 views
2

我有一個2D座標點。我需要改變點座標值以遵循循環路徑。在圓形路徑上移動一個點

我該如何實現使用C?

+2

你需要讓點移動周長? x和y的增量是多少?什麼半徑和中心點?這是功課嗎? –

+4

'#include '並使用'sin()'和'cos()'。你有什麼嘗試? – pmg

+0

描述使用極座標的路徑。 –

回答

6

你可以用極座標:

X = R * cos (phi) + center_X 
Y = R * sin (phi) + center_Y 

,改變披在循環。

11

使用正弦和餘弦

for (double t = 0; t < 2*Pi; t += 0.01) { 
    x = R*cos(t) + x_0; 
    y = R*sin(t) + y_0; 
} 

其中:

  • (X_0,y_0)是圓
  • R的中心是RADUIS
6

或者在角的代替弧度...

#include <math.h> 

void Circle(float center_x, float center_y, float radius) 
{ 
    float point_x, point_y; 
    int ctr; 
    for (ctr = 0; ctr < 360; ctr += 1) 
    { 
     point_x = radius * cos(ctr * 3.1415926f/180.0f) + center_x; 
     point_y = radius * cos(ctr * 3.1415926f/180.0f) + center_y; 
    } 
} 

圍繞一箇中心點繪製一個圓,每次1度。您可以通過增加ctr來調整步長。

+1

如果沒有**強烈**的原因,否則,更喜歡在C中使用'double'。你的代碼中的浮點變量和常量都應該是'double'類型。 – pmg

0

我相信你對y軸的sin()困惑了cos()。代碼應該是: point_y = radius * sin(ctr * 3.1415926f/180.0f)+ center_y;