0

下面的代码使用字符串数组中的对象绘制螺旋线。一切都很好,除了我希望在每个实例上以大约 45 度角绘制文本对象(基于下面代码中的当前 x、y 坐标)而不是水平绘制(当文本水平绘制时) ,它在曲线顶部和底部的集中点自然与其他文本重叠)。我研究了一些方法,但我对这一切还是很陌生,潜在的解决方案都避开了我。

String example = "";

String[] wordSet = split(example, " ");

float x, y;
float angle = 0;
float radiusSpiralLine = 10;

size (800, 800);
translate(width/2, height/2);
background(#ffffff);
smooth();
fill(0);

for (int i = 0; i < wordSet.length; i++) {


  angle += .05;
  radiusSpiralLine += .5;

  x = cos(angle) * radiusSpiralLine;
  y = sin(angle) * radiusSpiralLine;

  textSize(9);
  text(wordSet[i], x, y);

}
4

2 回答 2

1

这是非常相似问题的教程。基本上,您需要通过 pushMatrix() 存储投影矩阵,然后根据曲线上字母的位置平移和旋转,然后通过 popMatrix() 恢复矩阵。我不知道你究竟想如何旋转你的文字,但只是text()像这样折叠你的功能,也许它会帮助你:

pushMatrix();
translate(x, y);
rotate(angle);
text(wordSet[i], 0, 0);
popMatrix(); 
于 2013-10-31T10:56:11.713 回答
0

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

希望这可以帮助。

于 2013-10-31T18:00:52.813 回答