Using sequence of generated numbers to draw

Hi. I have some code that I'm having problems with. Within the code a sequence of 4 numbers are generated and then sorted in order i.e. 4, 12, 15, 25.

What I want to do is use these numbers and then draw a series of lines, so I am telling the code to take the first number generated (4) and then draw these lines before moving onto the next number and drawing that amount of lines (12) along a different y axis. So what it should do for example is draw 4 lines, then draw 12 lines at a different y axis and then 15 etc...

However, I have put the code that generates the numbers within void draw as I could not get it to work otherwise. But what this means is that it is continually looping and generated a new set of numbers. So what I need help with is making it so that only one set of numbers is generated. Hope that all makes sense. Any help would be appreciated.

Thanks,

Michael

ArrayList<Line> lines;

int z = 0;
int startx = 500;
int starty = 500;
int endy = 50;

void setup() {

  size(1000, 1000);

  lines = new ArrayList<Line>();
}

void draw() {

  background(255);

  for (int z = 0; z < lines.size (); z++) { 

    Line line = lines.get(z);
    line.draw();
  }

  int num =0;
  int[] numbers = new int[4];
  for (int x =0; x<4; x++) {

    num +=int(random(2, 50));
    int number = num % 60;
    print(num % 60  + ", ");
    numbers [x] = number;
  }

  numbers = sort(numbers);

  println("\nsorted");
  println(numbers);

  int a = numbers[0];
  int b = numbers[1];
  int c = numbers[2];
  int d = numbers[3];

  z++;

  if ( z > 0 && z < a ) {
    for (int x = 0; x < a; x++) 
      lines.add(new Line(startx, starty, random(50, 250), 50));
  }
  if ( z > a && z < b ) {
    for (int x = 0; x < b; x++) 
      lines.add(new Line(startx, starty, random(50, 250), 100));
  }
  if ( z > b && z < c ) {
    for (int x = 0; x < c; x++) 
      lines.add(new Line(startx, starty, random(50, 250), 150));
  }
  if ( z > c && z < d ) {
    for (int x = 0; x < d; x++) 
      lines.add(new Line(startx, starty, random(50, 250), 200));
  }
}

class Line {

  float sx;
  float sy;
  float ex;
  float ey;

  Line(float startX, float startY, float endX, float endY) {

    sx = startX;
    sy = startY;
    ex = endX;
    ey = endY;
  }

  void draw() {

    stroke(0);
    line(ex, ey, sx, sy);
  }
}  
Tagged:

Answers

  • edited February 2017

    put the definition of the member (line 26) before setup() making it global.

    fill the array (lines 27-38) inside setup() - only happens once.

    (untested)

  • Thank you! That did work! Thought that is frustrating as I could have sworn I tried that haha.

    However, it now seems I have another problem. It doesn't seem like the amount of lines being drawn is corresponding with the numbers being generated so it seems like my code between 40 and 61 needs some tweaking. So again any help would be much appreciated.

  • You should provide your latest updated code.

    Kf

  • edited February 2017

    Ok so this is the code at the moment. It now generates just the one set of numbers and then draws sets of lines in accordance with the numbers. However, what I would now like it to do is draw one set of lines before starting the next set. So for example if the numbers generated were 1, 10, 15 and 20 then it would draw 1 line before then drawing 10 lines at a different y axis etc...

    Any help would be great. It seems like it should be easy to get it to draw in a sequence though I have been pulling my hair out trying to work it out.

    ArrayList<Line> lines;
    boolean line2;
    boolean line3;
    boolean line4;
    
    int z = 1;
    int a = 0;
    int b = 0;
    
    int startx = 500;
    int starty = 500;
    int endy = 50;
    
    
    int[] numbers = new int[6];
    
    void setup() {
    
      size(1000, 1000);
      frameRate(4);
    
      lines = new ArrayList<Line>();
    
      int num =0;
      for (int x =0; x<6; x++) {
    
        num +=int(random(10, 50));
        int number = num % 60;
        print(num % 60  + ", ");
        numbers [x] = number;
      }
    
      numbers = sort(numbers);
    
      println("\nsorted");
      println(numbers);
    }
    
    void draw() {
    
      background(255);
    
      for (int z = 0; z < lines.size (); z++) { 
    
        Line line = lines.get(z);
        line.draw();
      }
    
    z++;
    
      if ( z  < numbers[0]) {
         lines.add(new Line(startx, starty, random(50, 250), 50));
      }  
      if ( z  < numbers[1]) {
         lines.add(new Line(startx, starty, random(50, 250), 100));
      }   
      if ( z  < numbers[2]) {
         lines.add(new Line(startx, starty, random(50, 250), 150));
      }   
      if ( z  < numbers[3]) {
         lines.add(new Line(startx, starty, random(50, 250), 200));
      }   
       if ( z  < numbers[4]) {
         lines.add(new Line(startx, starty, random(50, 250), 250));
      }   
       if ( z  < numbers[5]) {
         lines.add(new Line(startx, starty, random(50, 250), 300));
      }   
    
    }
    
    class Line {
    
      float sx;
      float sy;
      float ex;
      float ey;
    
      Line(float startX, float startY, float endX, float endY) {
    
        sx = startX;
        sy = startY;
        ex = endX;
        ey = endY;
      }
    
      void draw() {
    
        stroke(0);
        line(ex, ey, sx, sy);
      }
    }  
    
  • Answer ✓

    Here is my attempt. Please do not use reserved words for naming your variables, for example Line line since line is a function in Processing: https://processing.org/reference/line_.html

    If you want to add a different y value for each group of line, keep track the value of this y value and assign it instead of starty value in generateLines().

    Kf

    ArrayList<Line> lines;
    boolean line2;
    boolean line3;
    boolean line4;
    
    int z = 0;
    int a = 0;
    int b = 0;
    
    int startx = 500;
    int starty = 500;
    int endy = 50;
    
    int[] numbers = new int[6];
    
    void setup() {
    
      size(800, 800);
      frameRate(4);
      lines = new ArrayList<Line>();
    
      int num =0;
      for (int x =0; x<6; x++) {
        num +=int(random(10, 50));
        print((numbers [x]= (num % 60))  + ", ");
      }
      numbers = sort(numbers);
    
      println("\nsorted");
      println(numbers);
      fill(0);
      noLoop();
    }
    
    void draw() {
    
      background(255);
    
      text("Current "+z,width/2,height*0.80);
    
      int nn=0;
      for (int i=0; i<=z; i++)
        nn+=numbers[i];
    
      generateLines();
    
      for (int i = 0; i <nn ; i++) {   
        Line myline = lines.get(i);
        myline.draw();
      }
    }
    
    void generateLines() {
      if(z==0)
      lines.clear();
    
      for (int j=0;j<numbers[z];j++){
        lines.add(new Line(startx, starty, random(50, 250), 50));
      }
    
    }
    
    void mouseReleased() {
    
      z++;
    
      if (z==numbers.length-1)
        z=0;
    
        redraw();
    }
    
    class Line {
    
      float sx;
      float sy;
      float ex;
      float ey;
    
      Line(float startX, float startY, float endX, float endY) {
    
        sx = startX;
        sy = startY;
        ex = endX;
        ey = endY;
      }
    
      void draw() {
    
        stroke(0);
        line(ex, ey, sx, sy);
      }
    }
    
  • Thank you. That code looks good. I'll have a play around and let you know how I get on.

  • edited February 2017

    Thank you. I managed to play around with that and get it to do what I wanted. I also have some other features that I want to add but will start a new post for that as it is a bit of a different topic. But thank you all for your help. Here is how it looks now.

    ArrayList<Line> lines;
    
    int z = 0;
    int a = 0;
    int b = 0;
    
    int startx = 500;
    int starty = 500;
    int endy = 50;
    
    int[] numbers = new int[20];
    
    void setup() {
    
      size(1000, 1000);
      frameRate(4);
      lines = new ArrayList<Line>();
    
      int num =0;
      for (int x =0; x< 20; x++) {
        num +=int(random(10, 50));
        print((numbers [x]= (num % 60))  + ", ");
      }
      numbers = sort(numbers);
    
      println("\nsorted");
      println(numbers);
      fill(0);
    
    }
    
    void draw() {
    
      background(255);
    
      text("Current "+z,width/2,height*0.80);
    
      int nn=-1;
      for (int i=0; i<=z; i++)
        nn+=numbers[i] -1;
    
      generateLines();
    
      for (int i = 0; i <nn ; i++) {   
        Line myline = lines.get(i);
        myline.draw();
    
         }
    
      }
    
    
    void generateLines() {
    
      if (z < numbers.length -1 ) {
      z++; }
    
    
      for (int j=0;j<numbers[z] -1;j++){
    
        lines.add(new Line(startx, starty, random(50, 250), 50*z)); }
    
      } 
    
    class Line {
    
      float sx;
      float sy;
      float ex;
      float ey;
    
      Line(float startX, float startY, float endX, float endY) {
    
        sx = startX;
        sy = startY;
        ex = endX;
        ey = endY;
      }
    
      void draw() {
    
        stroke(0);
        line(ex, ey, sx, sy);
      }
    }
    
Sign In or Register to comment.