How to change String order and position and keep char order of each String?!

here you can click 1, 2 or 3



// NEW 



// https://discourse.processing.org/t/how-to-change-string-order-and-position-and-keep-char-order-of-each-string/23656


ArrayList<Letter> list = new ArrayList(); 

String  s = "one for all"; // Start String 

int[] order1 = new int[] {0, 1, 2}; // different word orders 
int[] order2 = new int[] {2, 1, 0};
int[] order3 = new int[] {2, 0, 1};

String message = "";

float   amt=1;

// -------------------------------------------------------------------------------------
//  the 2 core functions of processing 

void setup() {

  size(1000, 1000);

  // split String s
  String []st = split(s, ' ');
  // make s again but without the ' '
  message = "";
  for (int i=0; i<st.length; i++) {
    message += st[i];
  }

  println(message); 

  testOutputOneOrder(message, order1);

  //
} // setup

void draw() {
  background(0);

  for (Letter l : list) {
    l.display();
    l.move();
  }
}//draw

// ------------------------------------------------------------------------------------------

void keyPressed () {
  // Now test the function arrange (which is the core function) 

  if (key=='1') 
    testOutputOneOrder2(message, order1);
  else if  (key=='2')
    testOutputOneOrder2(message, order2);
  else if  (key=='3')
    testOutputOneOrder2(message, order3);

  amt=0;
}

// ------------------------------------------------------------------------------------------
// test function 

void testOutputOneOrder(String message_, int[] order1_) {
  // Now test the function arrange (which is the core function) 
  int[] newOrder = arrange(s, order1_);
  // printArrayMy(order1_);
  // printArrayMy(newOrder); 
  printWordMy(message_, newOrder);
  setWordMy(message_, newOrder);
  println("");
}

void testOutputOneOrder2(String message_, int[] order1_) {
  // Now test the function arrange (which is the core function) 
  int[] newOrder = arrange(s, order1_);
  // printArrayMy(order1_);
  //  printArrayMy(newOrder); 
  printWordMy(message_, newOrder);
  setWordMy2(message_, newOrder);
  println("");
}

// ------------------------------------------------------------------------------------------
//  the core function and other functions

int[] arrange(String incomingString_, int[] order) {
  // the core function!

  // the function should accept two things – a String, and an int[] word order number sequence – and it should return an int[] of the letter id order

  int[] result; 
  int len1=0;

  // split s  -------------------------------------------------
  String []st; // split String
  st = split(incomingString_, ' ');

  // build an array newArrayWords from st to keep track of letter positions   -------------------------------------------------
  OneWord[] newArrayWords = new OneWord[st.length]; 

  int j=0; 
  for (int i=0; i<st.length; i++) {  // for-loop over st
    newArrayWords[i] = new OneWord(st[i], j); // put this word AND its start position j into array newArrayWords (defining ow.arrayStart from j)
    j += st[i].length();
  }

  //  testPrintlnNewArrayWords(newArrayWords);

  if (newArrayWords.length != order.length) {
    println("Error +++++++++++++");
  }

  // prepare empty array result  -------------------------------------------------
  for (OneWord ow : newArrayWords) {
    len1 += ow.stringS.length(); // get the length len1 for the array
  }
  result = new int [ len1 + newArrayWords.length ]; 

  // fill array result -----------------------------------------------
  int indexOfResult=0;

  for (int i_order=0; i_order<order.length; i_order++) { // outer for-loop

    OneWord ow = newArrayWords[order[i_order]]; // get the word ow according to order 

    // read the letter numbers for ow starting at ow.arrayStart
    for (int i_Count_Char=0; i_Count_Char < ow.stringS.length(); i_Count_Char++) {  // inner for-loop
      result[ indexOfResult ] = ow.arrayStart + i_Count_Char; // 6,7,8 etc. 
      indexOfResult++;
    }//inner for-loop

    result[ indexOfResult ] = -1;
    indexOfResult++;
  }//outer for-loop

  // return the result -----------------------------------------------
  return result;
}// func 

//void testPrintlnNewArrayWords(OneWord[] newArrayWords_) {
//  for (OneWord ow : newArrayWords_) {
//    println ( ow.stringS, ow.arrayStart);
//  }
//  //println("");
//}//func 

//void printArrayMy(int[] list_) {
//  for (int i1 : list_) {
//    print(i1+", ");
//  }
//  println("");
//}//func 

void printWordMy(String s_, int[]newOrder_) {
  for (int i=0; i<newOrder_.length; i++) {
    if (newOrder_[i] != -1) {
      print(s_.charAt( newOrder_[i] ));
    } else {
      print(" ");
    }
  }//for
  println("");
}//func 


void setWordMy(String s_, int[]newOrder_) {
  for (int i=0; i<newOrder_.length; i++) {
    if (newOrder_[i] != -1) {
      list.add(new Letter(s_.charAt( newOrder_[i] ), i*44+271, 333));
    } else {
      //list.add(new Letter(' ', i*44, 333) );
    }
  }//for
  println("");
}//func 

void setWordMy2(String s_, int[]newOrder_) {
  int i2=0;
  for (int i=0; i<newOrder_.length; i++) {
    Letter l; 
    if (newOrder_[i] != -1) { 
      l = list.get(newOrder_[i]);
      //else {
      //l = list.get(i2);
      //}
      l.xTarget=i*44+271;
      l.yTarget=333;
      i2++;
    }
  }//for
  println("");
}//func 

// =================================================================================
// classes 

class Letter { 
  char letter; // the letter 
  float x, y; // , r, gx, gy;
  float x1, y1; 

  float xTarget, yTarget; 

  Letter( char _l, float _x, float _y) {

    x = _x;
    y = _y;
    x1=x;
    y1=y;
    letter=_l;
    // r=_r;
  }
  // constr

  void display() {
    //
    fill(255) ;
    text(letter, x1, y1);
  }

  void move() {
    if (amt==1) 
      return; 

    x1=lerp(x, xTarget, amt); 
    y1=lerp(y, yTarget, amt);
    amt+=0.0012;
    if (amt>=1) {
      x=xTarget;
      y=yTarget;
      amt=1;
    }
  }
}

// =================================================================================

class OneWord {

  String stringS; 
  int arrayStart; // start pos of this word in the original String 

  // constr 
  OneWord (String stringS_, int pos_) {
    stringS    = stringS_;
    arrayStart = pos_;
  }// constr
  // 
  // not methods in this class
  //
}//class 
//

1 Like

Thank you very much dear @Chrisir, now I understand why you’ve sent me the previous example with the lerp between positions inside the Letter class… I will look at your sketch carefully and will be back to you with few questions! You’ve saved my life once again.
I certainly want to make too complex projects with my very very basic knowledge and ‘skill’, sorry for that, but I am sure it will be nice at the end thanks to you :))
All the best,
L

1 Like