Connecting plot points issue

I want to create a line that with one point at (0,0) and whose other point is constantly moving. Initially I would like to generate two separate random plots points, and then have one end of the line move from (x1, y1) to (x2,y2), and then once it reaches that point, create another plot point for which the end of the line moves to from (x2,y2).

Essentially, I am trying to make it like a random connect the dots. However, the line always jumps instead of continuing where it left off. I am new to processing and this is just a simple exercise I created for myself, but its proving to be more complicated than I thought. I feel like I am over thinking this.

float x;
float y;
float x1 = floor(random(100, 400));
float y1 = floor(random(100, 400));
float x2 = floor(random(100, 400));
float y2 = floor(random(100, 400));
float i = 0;

void setup() {
  size(600, 600);
}

void draw() {

  if (x1 == x2) {
    x2 = floor(random(200, 300));
    y2 = floor(random(200, 300));
  }
  background(255);

  float m = ((x1-x2)/(y1-y2));
  float b = (y1-(m*x1));


  i=frameCount % 100;

  if (x1<x2) {
    x = x1+((i/100)*abs(x1-x2)); 
    y = m*x+b;
    line(0, 0, x, y);
  }

  if (x2<x1) {
    x = x2+((i/100)*abs(x1-x2)); 
    y = m*x+b;
    line(0, 0, x, y);
  }
  println(x1, x2, i, x, y, m, b);

  if (frameCount % 100 == 0) {
    x1 = x2;
    y1 = x2;
  }
}

Answers

  • Like this you want ?

    float[] x = new float[10];
    float[] y = new float[10];
    
    
    void setup() {
      size(600, 600);
      for (int i=0;i<x.length;i++) {
        x[i] = 50 + 100*random(1);
        y[i] = 50 + 100*random(1);
      }
    }
    
    void draw() {
      background(0);
      noFill();
      stroke(-1);
      beginShape();
      for (int i=0;i<x.length;i++) {
        vertex(x[i], y[i]);
      }
      endShape();
    }
    
Sign In or Register to comment.