Processing 加工旋转线

Processing 加工旋转线,processing,Processing,我试图通过处理来实现以下内容,但两行之间的间距不均匀 我使用了以下代码 void setup(){ size(300,300); rectMode(CENTER); background(0); translate(150,150); for(int i=0;i<360;i+=15){ rect(0,100,5,50); rotate(i); } } void setup(){ 尺寸(300300); 矩形模式(中心); 背景(0); 翻译(150150);

我试图通过处理来实现以下内容,但两行之间的间距不均匀

我使用了以下代码

void setup(){
size(300,300);
rectMode(CENTER);
background(0);
translate(150,150);
for(int i=0;i<360;i+=15){
    rect(0,100,5,50);

    rotate(i);
    }
}
void setup(){
尺寸(300300);
矩形模式(中心);
背景(0);
翻译(150150);

对于(int i=0;iOK),这里发生的是: 您正在使用rotate(i),其中i以度为单位。rotate()采用弧度。 要解决此问题,请使用
旋转(弧度(i))
将i从度转换为弧度,然后旋转。 还有:旋转是累积的。第一次旋转0度,第二次旋转15度。第三次旋转30度,现在是45度。 看起来是这样的:

i=0: 0
i=1: 0
i=2: 15
i=3: 45
i=4: 90
i=5: 150
i=6: 225
i=7: 315
i=8: 420
i=9: 540
i=10: 675
i=11: 825
i=12: 990
如您所见,循环中的每次迭代间隔都会增加。要解决此问题,循环有多个选项:

for(int i=0;i<360;i+=15){
    rotate(radians(i));//do rotation to apply to the rectangle, converting i to radians
    rect(0,100,5,50);//draw rectangle
    rotate(radians(-i));//undo rotation for next iteration, converting i to radians
}

for(int i=0;i其他答案/注释都很好,尽管您可以这样做

void setup(){
  size(300, 300);
  translate(150, 150);
  int count = 12;               //you can change this to any integer
  for(int i=0;i<count;i++){
    rect(0,100,5,50);
    rotate(radians(360/count));
  }
}
void setup(){
大小(300300);
翻译(150150);
int count=12;//您可以将其更改为任何整数

对于(int i=0;旋转的效果是累积的。您可以查看
pushMatrix
popMatrix
——或者您可以重复旋转15度(此外,您不想混淆度和弧度,这似乎是您在做的)。
pushMatrix();//重置当前旋转和平移
您的注释没有真正意义。
pushMatrix
存储当前坐标系——它不会重置任何内容。如果初始平移在第一个
pushMatrix
之前,则无需不断重新转换(就像OP的代码中那样)。如果使用for循环替换OP的for循环,您将看到它无法按预期工作,因为最终结果将是
translate(300300)
。编辑后这仍然是错误的。
popMatrix()
不会撤消任何转换,除非在相应的
pushMatrix()之后有转换
。问题不仅仅在于你的评论不正确——你的代码中有一个实际的错误。在OP的代码中,
translate(150150)
在循环之前,因此在
pushMatrix()
之前。因此,通过
translate(150150)跟随
pushMatrix()
是代码中的一个错误
应删除该行,并从注释中删除所有对翻译的引用。您正在推动和弹出旋转。上一个翻译与此无关。
void setup(){
  size(300, 300);
  translate(150, 150);
  int count = 12;               //you can change this to any integer
  for(int i=0;i<count;i++){
    rect(0,100,5,50);
    rotate(radians(360/count));
  }
}