2016-11-16 6 views
0

次のコードで円を描こうとしています。私はそれを作成するために変更する必要がある値を把握できません。助けをお待ちしています。処理中に円を作成する方法がわからない

void setup() { 

    size(400, 400); 
    background(255,255,255); 
} 

void draw() { 

    float step=(2*PI)/120;        

    float theta_start=0;         

    float old_sx=map(theta_start,0,2*PI,4,width-4);  
    float old_sy1=map(sin(theta_start),-1,1,height-4,4); 
    float old_sy2=map(cos(theta_start),0,1*PI,1,width-2); 

    for(float theta=step;theta<=(2*PI)+step;theta+=step) 
    { 
    float screen_x=map(theta,0,2*PI,4,width-4);  
    float screen_y1=map(sin(theta),-1,1,height-4,4); 
    float screen_y2=map(cos(theta),0,1*PI,1,width-2); 

    //stroke(255,0,0); 
    //line(old_sx,old_sy1,screen_x,screen_y1);   

    //stroke(0,255,0); 
    // line(old_sx,old_sy2,screen_x,screen_y2);  
    stroke(0,0,255); 
    line(old_sy1,old_sy2,screen_y1,screen_y2); 

    old_sx=screen_x;         
    old_sy1=screen_y1; 
    old_sy2=screen_y2; 

    } 
} 
+1

'ellipse()'関数を呼び出してみませんか? –

答えて

3

半径1の円は

に位置する全ての点(x、y)は、(罪(シータ)、COS(シータ))として定義することができる

すべて0 < = theta < 2 * PI。

半径を変更するために、単に

(半径* SIN(シータ)、半径* COS(シータ))にそれを変えます。

最後に

、あなただけのこれらの追加、位置(POSX、POSY)に半径の中心を変更する場合:

(半径*罪(シータ)+ POSX、半径* COS(シータ)+ posY)

void setup() 
{ 
    size(400, 400); 
    background(255,255,255); 
} 

void draw() 
{ 
    float step=(2*PI)/120; 
    int posX = width/2; 
    int posY = height/2; 
    float radius = 100; 
    int xOld=0, yOld=0; 
    for(float theta=0;theta<=(2*PI)+step;theta+=step) 
    { 
    stroke(0,0,255); 
    int x = int(radius*sin(theta) + posX); 
    int y = int(radius*cos(theta) + posY); 
    if(theta>0) 
    { 
     line(x,y,xOld,yOld); 
    } 
    xOld = x; 
    yOld = y; 
    } 

} 
関連する問題