处理旋转线

问题描述 投票:1回答:2

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

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

processing
2个回答
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.
}

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));
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.