[Answered] Growing random lines

edited December 2015 in Questions about Code

Hi everyone! I'm trying to make random lines that grows one after another. I got this far:

int xrand = 300;
int yrand = 300;
int py = 0;
int px = 0;
int x;
int y;

void setup () {
  size (500,500);
  smooth();
}

void draw () {
  if (x<xrand) {
    x = x + 1;
  } else if (x>xrand) {
    x = x - 1;
  } else {
    px = x;
    xrand = (int) random(0,width);
  }
  if (y<yrand) {
    y = y + 1;
  } else if (y>yrand) {
    y = y - 1;
  } else {
    py = y;
    yrand = (int) random(0,height);
  }
  line (px,py,x,y);
}

void mousePressed () {
  println(xrand,yrand,px,py,x,y);
}

The problem is that when the lines are growing they're drawing "every single step" but i only want the straight line. Anyone know how to solve it? Thanks!!

Answers

  • edited December 2015

    Like this?

    int xrand = 300;
    int yrand = 300;
    int py = 0;
    int px = 0;
    int x;
    int y;
    
    void setup() {
        size(500, 500);
        smooth();
        frameRate(10);
    }
    
    void draw() {
        x = xrand;
        y = yrand;
        line(px, py, x, y);
        xrand = (int) random(0, width);
        yrand = (int) random(0, height);
        px = x;
        py = y;
    }
    
    void mousePressed() {
        println(xrand, yrand, px, py, x, y);
    }
    
  • edited December 2015

    Thanks Ater! Kind of... i want that with processing drawing the lines little by little and not all of sudden. That's why i put the x = x + 1;

    Maybe i misspelled the problem: i didn't want the "between lines" when i was drawing the random lines. Those "between line" are drawing shapes and i only want the line(px,py,x,y);

    How can i solve it?

  • look at lerp() to make a line grow slowly...

Sign In or Register to comment.