1

I've got an array of strings that I'd like to draw as a giant spiral. I only have a very, very rough idea of where to begin. First, I will probably need to break the strings into an array of individual characters? Next, I will probably need to apply the following geometry in order to generate the spiral shape?

float r = 0;
float theta = 0;

void setup() {
  size(200,200);
  background(255);
}

void draw() {

  float x = r * cos(theta);
  float y = r * sin(theta);

  noStroke();
  fill(0);
  ellipse(x+width/2, y+height/2, 6, 6); 

  theta += 0.01;
  r += 0.05;
}

However, I don't know how to step through my array of characters in order to draw them in a spiral-like format. I apologize for the lack of clarity. Any suggestions would be awesome! I'm very new to all of this (clearly).

4

1 回答 1

2

您创建螺旋的代码是个好主意。创建旋转文本的一种方法是使用rotate(),结合text()。您将有一个 for 循环,遍历您的字符数组,增加半径,然后以这种方式绘制文本。请注意,rotate()具有累积效应。就像是:

String str = "asdflkkjsahfdlkadshflkahdslkfajsdf";
float radius = 0;
//so we are rotating around the center, rather than (0,0):
translate(width/2, height/2); 
for (int i = 0; i < str.length(); i++) {
  radius += 2;
  // taken out because of non-constant spacing at large radius:
  //rotate(0.5);
  // this should give constant spacing, no matter the radius
  // change 10 to some other number for a different spacing. 
  rotate(10/radius);
  // drawing at (0,radius) because we're drawing onto a rotated canvas
  text(str.charAt(i), 0, radius);
}

您可能希望角度变化是半径的函数,因为在大半径下,字母间隔非常远。一种方法是使用方程s = rθ,其中 s 是弧长(在这种情况下,字母之间的距离),r 是半径,θ 是角度变化。如果您想要字母之间的恒定距离,无论半径如何,那么 θ 必须与 1/r 成正比。当然,您可以根据自己的喜好调整硬编码值。

另外:rotate()translate()方法在 结尾处被撤消draw(),所以如果你在这段代码之后没有做任何事情,那么保持原样是可以的。如果您确实想在之后绘制更多东西,那么您必须在绘制其他任何东西之前手动撤消旋转和平移。

编辑:我刚刚意识到我假设您希望字母也被旋转,而不仅仅是定位在螺旋中,而且仍然正常定向。在这种情况下,您可以使用现有代码并用适当的参数替换ellipse(...)with 。text(str.charAt(...)...)

于 2013-10-01T17:08:40.083 回答