首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不知道如何在加工过程中做圆圈

不知道如何在加工过程中做圆圈
EN

Stack Overflow用户
提问于 2016-11-16 21:40:01
回答 1查看 190关注 0票数 0

我正在试着用下面的代码做一个圆圈。我只是不知道我需要改变什么值才能创建它。感谢任何人的帮助。

代码语言:javascript
复制
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;

  } 
}
EN

回答 1

Stack Overflow用户

发布于 2016-11-17 00:33:50

半径为1的圆可以定义为位于以下位置的所有点(x,y

(sin(theta),cos(theta))

适用于所有0<=theta<2*PI。

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

半径(*sin(θ),radius * cos(theta)).

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

半径(*sin(θ)+ posX,radius * cos(theta) + posY)

代码语言:javascript
复制
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;
  }

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40633621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档