无法弄清楚如何在Processing中制作圆圈

时间:2016-11-16 13:40:01

标签: processing

我正在尝试使用以下代码制作一个圆圈。我只是想弄清楚我需要改变什么值来创建它。感谢任何帮助。

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 个答案:

答案 0 :(得分:3)

半径为1的圆可以定义为位于

的所有点(x,y)

(sin(theta),cos(theta))

表示所有0&lt; = theta&lt; 2 * PI。

要更改半径,只需将其更改为

即可

(radius * sin(theta),radius * cos(theta))。

最后,如果您想将半径的中心更改为某个位置(posX,posY),只需添加以下内容:

(radius * sin(theta)+ posX,radius * cos(theta)+ 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;
  }

}