我正在试着用下面的代码做一个圆圈。我只是不知道我需要改变什么值才能创建它。感谢任何人的帮助。
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;
}
}发布于 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)
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;
}
}https://stackoverflow.com/questions/40633621
复制相似问题