处理旋转线

时间:2018-06-05 18:13:06

标签: processing

我正在尝试使用处理进行以下操作,但两条线之间的间距不均匀

Sample Image

我使用了以下代码

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);
    }
}

但我得到以下结果

sample Output

2 个答案:

答案 0 :(得分:1)

行。那么这里发生的是: 你正在使用rotate(i),其中我是度数。 rotate()取弧度。 要解决此问题,请使用rotate(radians(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<360;i+=15){
    pushMatrix();//store current translation and rotation and start rotations/translations from default coordinate system
    translate(150,150);//redo the translation that pushMatrix() put away
    rotate(radians(i));//do the rotation, converting i to radians
    rect(0,100,5,50);//draw the rectangle
    popMatrix();//pop the matrix, now all the translations we just did are forgotten and the translation before pushMatrix() outside of the loop is kept.
    // but the rectangle we drew keeps the translations.
}

答案 1 :(得分:0)

其他答案/评论很好,但你可以这样做......

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));
  }
}