Processing 如何在处理过程中以一定角度绘制字符串对象?

Processing 如何在处理过程中以一定角度绘制字符串对象?,processing,Processing,下面的代码使用字符串数组中的对象绘制螺旋。一切都很好,只是我希望文本对象在每个实例上都以大约45度的角度绘制(基于下面代码中当前的x,y坐标),而不是水平绘制(当文本水平绘制时,它自然会在曲线顶部和底部的集中点与其他文本重叠). 我研究了一些方法,但我对所有这些都还很陌生,潜在的解决方案都回避了我 String example = ""; String[] wordSet = split(example, " "); float x, y; float angle = 0; float r

下面的代码使用字符串数组中的对象绘制螺旋。一切都很好,只是我希望文本对象在每个实例上都以大约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);

}
String-example=”“;
String[]wordSet=split(例如,“”);
浮动x,y;
浮动角度=0;
浮动半径螺旋线=10;
大小(800800);
平移(宽度/2,高度/2);
背景(#ffffff);
光滑的();
填充(0);
for(int i=0;i
非常类似的问题。在basic中,您需要通过pushMatrix()存储投影矩阵,然后根据字母在曲线上的位置进行平移和旋转,然后通过popMatrix()恢复矩阵。我不知道您希望如何旋转文本,但只需像这样折叠
text()
函数,它可能会帮助您:

pushMatrix();
translate(x, y);
rotate(angle);
text(wordSet[i], 0, 0);
popMatrix(); 

首先,您应该养成在
setup()
draw()函数中包装代码的习惯。由于您正在绘制静态图像,因此不需要
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() {
}
稍微好一点,但还没有达到:

  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
for(int i=0;i

希望这有帮助

当我采纳你的建议时,它似乎只是旋转整个图形,而不是旋转沿曲线绘制每个单独对象的程度
  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