Facebook
From Evangelos Nikolaou, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 131
  1. void setup() {
  2.   size(600, 600);
  3.   fill(0);  
  4.  
  5.   // 1. populate an array with random characters
  6.   int num = 10;
  7.   char[] chars = populate(num);
  8.  
  9.   // 2. render characters
  10.   renderChars(chars);
  11.  
  12.   // 3. get a reversed string
  13.   String rev = combine(chars);
  14.   renderString(rev);
  15.  
  16.   // 4. shuffle initial array
  17.   char[] rchars = randomise(chars);
  18.   renderChars2(rchars);
  19.  
  20.   save("assignment7.png");
  21. }
  22.  
  23. char[] populate(int num) {
  24.   char[] chars = new char[num];
  25.   for(int i=0; i < num; i++) {
  26.     char c = (char)random(97,122);
  27.     chars[i] = c;
  28.     print(c);
  29.   }
  30.  
  31.   return chars;
  32. }
  33.  
  34. void renderChars(char[] chars) {
  35.   fill(0);
  36.   textSize(16);
  37.   text("1. Rendering " + chars.length + " random characters: ", 20, 70);
  38.   for(int i=0; i < chars.length; i++) {
  39.     text(chars[i], 20 + (i*10), 100);
  40.   }
  41. }
  42.  
  43. String combine(char[] chars) {
  44.   String s = "";
  45.   for(int i=chars.length - 1; i >= 0; i--) {
  46.     s += chars[i] + " ";
  47.   }
  48.  
  49.   return s;
  50. }
  51.  
  52. void renderString(String s) {
  53.   fill(0);
  54.   textSize(16);
  55.   text("2. Rendering reversed string: ", 20, 140);
  56.   text(s, 20 , 170);
  57. }
  58.  
  59. char[] randomise(char[] chars) {
  60.   char[] rchars = new char[chars.length];
  61.  
  62.   for(int i=0; i < chars.length; i++) {
  63.     int j = (int)random(0, chars.length);
  64.     char c = rchars[j];  
  65.  
  66.     while(c != '\u0000') {
  67.       j = (int)random(0, chars.length);
  68.       c = rchars[j];
  69.     }
  70.  
  71.     rchars[j] = chars[i];
  72.   }
  73.  
  74.   return rchars;
  75. }
  76.  
  77. void renderChars2(char[] chars) {
  78.   fill(0);
  79.   textSize(16);
  80.   text("3. Rendering with random order: ", 20, 210);
  81.   for(int i=0; i < chars.length; i++) {
  82.     text(chars[i], 20 + (i*10), 240);
  83.   }
  84. }
  85.