1

I have tried many methods, and can't seem to grasp the idea of extracting an index from my array of strings to help me generate my desired number of building with a desired height, please help, here is my example

edit: Hi, i saw your feedback and posted my code below, hopefully it helps with the idea overall, as much as it is just creating rects, its more complicated as i need to involve arrays and string splitting along with loops. i more or less got that covered but i as i said above, i cannot extract the values from my array of string and create my facades at my own desired number and height

String buffer = " ";
String bh = "9,4,6,8,12,2";
int[] b = int(split(bh, ","));
int buildingheight = b.length;

void setup () {
  size(1200, 800);
  background(0);
}

void draw () {
}
  
void Textbox() {
  textSize(30);
  text(buffer, 5, height-10);
}

void keyTyped() {
  if (key == BACKSPACE) {
    if (buffer.length() > 0) {
      buffer = buffer.substring(0, buffer.length() - 1);
    }
  } else if (key == ENTER) {
      background(0);
     
      stroke(0);
      GenerateFacade();
      println(buffer);
      
  }
    else {
    buffer = buffer + key;
    Textbox();
  }
}

void GenerateFacade() {
  fill(128);
   for (int i = 0; i < b.length; i++) {
     for (int j = 0; j < b.length; j++) {
       if (int(b[j]) > buildingheight) {
          buildingheight = int(b[j]);
       }
     }
        rect(i*width/b.length, height - (int(b[i])*height/buildingheight), width/b.length, int(b[i])*height/buildingheight);
   }
      
}
seawhale05
  • 11
  • 3
  • At least, what did you try? Because this is just about creating rects – Alex Cio Aug 09 '22 at 22:25
  • I think combining your and my solution should do the job. It’s also easier at the beginning to work with variables instead of using more than one loop for the rects. At least it makes it easier to read sometimes. – Alex Cio Aug 10 '22 at 07:20

1 Answers1

0

For the next time it would be great if you provide us with some code so we know what you tried and maybe can point you to the problem you have.

You need just the keyPressed function and some variables

int x = 0;
int buildingWidth = 50;
int buildingHeight = height / 6;

void setup(){

  size(1000, 500);
  background(255);
}

void draw(){
}

void keyPressed(){
  
   if(key >= '0' && key <= '9'){
      int numberPressed = key - '0' ;

      fill(0);
      rect(x, height - buildingHeight * numberPressed, 
           buildingWidth, buildingHeight * numberPressed);
      x += buildingWidth;
   }
  
}

This is my result

enter image description here

Alex Cio
  • 6,014
  • 5
  • 44
  • 74