随机字符串处理

Random character String processing

所以,我想用字符(字母)制作一条垂直线,以创建类似于 Matrix 效果的东西。 我从一个数字字符串开始,只是想看看它是否有效并且确实有效

String character = str (floor (random(10)));
this.letter = character;

现在我想让它有字母而不是数字,但我现在不知道如何让它随机生成。我尝试使用 char 和 string,但它显示了不止一个字母

character += char (int(random(65, 65+24)));

我已经尝试了一种推荐的新方法,但是处理崩溃并且没有 运行 它

  PVector pos;
  float speed;
  String letter;
  float change_threshold = 0.1;
  color cor = color (3, 160, 98);
  String character = "";
  // maximum letters in a string
  int maxLetters = 35;
  // which character to swap
  int charIndex = 0;

  Letter (float xpos, float ypos, float vel) {

    this.pickLetter();
    pos = new PVector (xpos, ypos);
    speed = vel;
  }

  void display() {
    for (int i = 0; i < maxLetters; i++) {
      character +=getRandomLetter() +"\n";
    }
    fill(this.cor);
    text(this.letter, this.pos.x, this.pos.y);
    float p = random(1);
    if (p < this.change_threshold && this.cor != color(255)) {
      this.pickLetter();
    }
  }

  void pickLetter() {

    //String character = str (floor (random(10)));
    //String character = new String ("a");
    //character += char (int(random(65, 65+24)));
    char randomChar = getRandomLetter();
    character = setCharAt(character, randomChar, charIndex);
    charIndex = (charIndex + 2)%character.length();
    this.letter = character;
  }


  void fall() {
    this.pos.y += this.speed;
  }

  // returns a random a-z char
  char getRandomLetter() {
    return char (int(random(65, 65+24)));
  }

  // return a new string with a char swapped at the given index
  String setCharAt(String myString, char myNewChar, int myCharIndex) {
    return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1);
  }
}

char (int(random(65, 65+24)));确实是随机字母的正确方法

character += char (int(random(65, 65+24))); 意味着您一次 append/concatenate 一个字母,因此您的 character 变量每次迭代都会增加一个新字符。

character = char (int(random(65, 65+24))); 每次迭代都会用一个新的随机数替换当前字符。

如果要制作竖排文本,可以使用换行符 (\n)。 不幸的是,你不能轻易地用 String class 换掉一个字符,但是用一点 substring() and concatenation you can simulate something similar. (The StringBuilder java class 会使字符交换更容易)。这是一个使用 String:

的注释示例
// full string of letters
String letters = "";
// maximum letters in a string
int maxLetters = 12;
// which character to swap
int charIndex = 0;

void setup(){
  size(300, 300);
  fill(0, 192, 0);
  textAlign(CENTER);
  textFont(createFont("Courier New", 12), 12);
  // populate the string
  for(int i = 0 ; i < maxLetters; i++){
    letters += getRandomLetter() + "\n";
  }
}

void draw(){
  // pick random char
  char randomChar = getRandomLetter();
  // replace existing characters
  letters = setCharAt(letters, randomChar, charIndex);
  // increment the char index by 2 to include \n
  // use the modulo operator to loop back to 0
  charIndex = (charIndex + 2) % letters.length();
  
  // render the text
  background(0);
  text(letters, width * 0.5, height * 0.25);
}

// returns a random a-z char
char getRandomLetter(){
  return char (int(random(65, 65+24)));
}

// return a new string with a char swapped at the given index
String setCharAt(String myString, char myNewChar, int myCharIndex){
  return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1); 
}

更新 以上可以封装复用:

MText text = new MText();

void setup(){
  size(300, 300);
  fill(0, 192, 0);
  textAlign(CENTER);
  textFont(createFont("Courier New", 12), 12);  
}

void draw(){
  background(0);
  text.draw();
}

class MText{
  // full string of letters
  String letters = "";
  // maximum letters in a string
  int maxLetters = 12;
  // which character to swap
  int charIndex = 0;
  
  float x, y;
  
  MText(){
    // populate the string
    for(int i = 0 ; i < maxLetters; i++){
      letters += getRandomLetter() + "\n";
    }
    // default position
    x = width * 0.5;
    y = height * 0.25;
  }
  
  void draw(){
    // pick random char
    char randomChar = getRandomLetter();
    // replace existing characters
    letters = setCharAt(letters, randomChar, charIndex);
    // increment the char index by 2 to include \n
    // use the modulo operator to loop back to 0
    charIndex = (charIndex + 2) % letters.length();
    // render text
    text(letters, x, y);
  }
  
  // returns a random a-z char
  char getRandomLetter(){
    return char (int(random(65, 65+24)));
  }
  
  // return a new string with a char swapped at the given index
  String setCharAt(String myString, char myNewChar, int myCharIndex){
    return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1); 
  }


}

grouping/encapuslating class 中的功能的优点是可以轻松管理多个实例:

int numTexts = 60;
ArrayList<MText> texts = new ArrayList<MText>();

void setup(){
  size(300, 300);
  fill(0, 192, 0);
  textAlign(CENTER);
  textFont(createFont("Courier New", 12), 12);
  for(int i = 0 ; i < numTexts; i++){
    MText text = new MText();
    text.x = random(width);
    text.y = random(height);
    texts.add(text);
  }
}

void draw(){
  background(0);
  for(MText text: texts) text.draw();
}

class MText{
  // full string of letters
  String letters = "";
  // maximum letters in a string
  int maxLetters = 12;
  // which character to swap
  int charIndex = 0;
  
  float x, y;
  float vy;
  float textHeight;
  
  MText(){
    // populate the string
    for(int i = 0 ; i < maxLetters; i++){
      letters += getRandomLetter() + "\n";
    }
    // default position
    x = width * 0.5;
    y = height * 0.25;
    // default Y velocity
    vy = random(.16018, 2.1);
    textHeight = (textAscent() - textDescent()) * maxLetters;
  }
  
  void draw(){
    // pick random char
    char randomChar = getRandomLetter();
    // replace existing characters
    letters = setCharAt(letters, randomChar, charIndex);
    // increment the char index by 2 to include \n
    // use the modulo operator to loop back to 0
    charIndex = (charIndex + 2) % letters.length();
    // update position
    y += vy;
    if(y > height + textHeight){
      y = -textHeight * 2;
      vy = random(.16018, 2.1);
    }
    // render text
    fill(0, 192, 0);
    text(letters, x, y);
    text(0, 255, 0);
    text(letters.charAt(0), x, y);
  }
  
  // returns a random a-z char
  char getRandomLetter(){
    return char (int(random(65, 65+24)));
  }
  
  // return a new string with a char swapped at the given index
  String setCharAt(String myString, char myNewChar, int myCharIndex){
    return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1); 
  }


}

希望以上是一个有用的方向。有很多方法可以达到类似的结果。在改进方面,一些小改动,例如一个更亮的绿色字符(甚至可能让它发光)、对齐 x 文本使其不重叠矩阵 1 效果、fading/leaving 轨迹和改变相机 z 位置矩阵2效应等

更新 2 更新后的代码不包括实例化 Letter 和渲染它的部分。我遇到的错误是由于字符串被初始化为空 (String character = "";) 并且随机字母函数无法替换不存在的索引处的字符(尚未)。这种情况下的解决方案是先用几个字符初始化字符串。 例如移动:

for (int i = 0; i < maxLetters; i++) {
      character +=getRandomLetter() +"\n";
    }

在构造函数中 before pickLetter() 被调用。 完整示例:

Letter l = new Letter(150, 15, 1.5);

void setup(){
  size(300, 300);
}

void draw(){
  background(0);
  l.display();
}

class Letter{
   PVector pos;
  float speed;
  String letter;
  float change_threshold = 0.1;
  color cor = color (3, 160, 98);
  String character = "";
  // maximum letters in a string
  int maxLetters = 35;
  // which character to swap
  int charIndex = 0;

  Letter (float xpos, float ypos, float vel) {
    for (int i = 0; i < maxLetters; i++) {
      character +=getRandomLetter() +"\n";
    }
    this.pickLetter();
    pos = new PVector (xpos, ypos);
    speed = vel;
  }

  void display() {
    
    fill(this.cor);
    text(this.letter, this.pos.x, this.pos.y);
    float p = random(1);
    if (p < this.change_threshold && this.cor != color(255)) {
      this.pickLetter();
    }
  }

  void pickLetter() {

    //String character = str (floor (random(10)));
    //String character = new String ("a");
    //character += char (int(random(65, 65+24)));
    char randomChar = getRandomLetter();
    character = setCharAt(character, randomChar, charIndex);
    charIndex = (charIndex + 2)%character.length();
    this.letter = character;
  }


  void fall() {
    this.pos.y += this.speed;
  }

  // returns a random a-z char
  char getRandomLetter() {
    return char (int(random(65, 65+24)));
  }

  // return a new string with a char swapped at the given index
  String setCharAt(String myString, char myNewChar, int myCharIndex) {
    return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1);
  }
}

使用 IntList 保存用于创建字母的整数的替代方法。列表中的数字和相应的字母随着每个 draw() 循环依次改变; frameRate 可能会减慢到 1 以查看更改。

IntList  charNum;
int y = 0;
int index = 0;

void display () {
  background(0);
  y = 30;
  for (int i = 0; i < charNum.size(); i++) {
    char a = char(charNum.get(i));
    fill(0, 192, 0);
    text(a, 60, y);
    y+=20;
  }
}

void setup() {
  size(200, 300);
  background(209);
  charNum = new IntList();
  for (int i = 0; i < 12; i++) {
    charNum.append(int(random(65, 65+24)));
  }
  display();
}

void draw() {
  frameRate(60); // Slow this to 1 to see changes
  charNum.set(index, int(random(65, 65+24)));
  display();
  index += 1;
  if (index > charNum.size() - 1) {
    index = 0;
  }
}