setup()
首先,您应该开始养成在anddraw()
函数中包装代码的习惯。由于您正在绘制静态图像,因此您不需要该draw()
功能,但我认为拥有这两个功能是一种很好的做法。
现在,您现在所做的只是将单词翻译成很小的量。算一算:
x = cos(angle) * radiusSpiralLine; //cos(.05)*.5 = .499
y = sin(angle) * radiusSpiralLine; //sin(.05)*.5 = .024
这意味着它们移动不到一个像素,而且它们根本没有旋转。
你需要的是你的好朋友,这个rotate()
功能。
让我们重新编写代码:
String example = "These are a bunch of words going around!";
String[] wordSet = split(example, " ");
float x, y;
float angle = 0;
void setup() {
size (800, 800);
background(#ffffff);
smooth();
fill(0);
pushMatrix();
translate(width/2, height/2); //Translate when you need to translate, not before
for (int i = 0; i < wordSet.length; i++) {
angle = PI/5; //Our good friends, radians
textSize(20); //What is this, text for ants? Change to 20
rotate(angle);
text(wordSet[i], 20, 0);
}
popMatrix();
}
void draw() {
}
首先注意,setup()
and draw()
。我喜欢他们那里。它看起来更好,我认为。
有几件重要的事情需要注意。rotate()
和translate()
在画布上的效果是累积的。我们可以通过不同的方式产生相同的效果:
for (int i = 0; i < wordSet.length; i++) {
angle = PI/5;
textSize(20);
rotate(angle); //always rotating by PI/5 ON TOP of previous rotation
text(wordSet[i], 20, 0);
}
//Everything from now on will still be rotated, we don't want that!
稍微好一点,但还没有:
for (int i = 0; i < wordSet.length; i++) {
angle += PI/5; //constantly increasing the angle
textSize(20);
pushMatrix(); //push a new canvas on top of everything
rotate(angle); //rotate by angle (which increases every loop)
text(wordSet[i], 20, 0);
popMatrix(); //pop the rotated canvas out, go back to original canvas
} //Things won't be rotated, but they'll still be translated, since translate() is outside of pushMatrix and popMatrix
希望这可以帮助。