Why are my lines moving the same?

Hello,

I am trying to figure out why my 2 lines are moving the same. I thought that “for every i” the line move different because of the ‘random’ a.

This is my first what own written code. My experience with coding before this was none. Hopefully some one can point out what i’m not thinking of.

int[] x1 ={100,300};
int[] y1 ={500,200};

float a;

void setup() {
  size(500, 750);
  background(255);
  stroke(0);
  
}

void draw() {
  for (int i=0; i<x1.length; i++) {

    float newx = x1[i] + cos(a) * 5;
    float newy = y1[i] + sin(a) * 5;
    line(x1[i], y1[i], newx, newy);
    x1[i] = int(newx);
    y1[i] = int(newy);
    a = a + random(-0.4, 0.4);
    
    if(newx < 0 || newy < 0 || newx > width || newy > height) {
      noLoop();
    }
  }
}
2 Likes

OR you work with 2 different a’s in an array


float[] x1 ={100, 300};
float[] y1 ={500, 200};

float[] a=new float [2];

void setup() {
  size(500, 750);
  background(255);
  stroke(0);

  a[0]=0; 
  a[1]=0;
}

void draw() {
  for (int i=0; i<x1.length; i++) {

    a[i] = a[i] + random(-1, 1);

    float newx = x1[i] + cos(a[i]) * 5;
    float newy = y1[i] + sin(a[i]) * 5;

    line(x1[i], y1[i], 
      newx, newy);
    x1[i] = (newx);
    y1[i] = (newy);


    if (newx < 0 || newy < 0 || newx > width || newy > height) {
      noLoop();
    }
  }
}
1 Like

Hello,

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate and use them.

This is a very short list:

Resources < Click here to expand !

I encourage you to review the resources available here:

:)

Consider using functions…

int[] x1 = {300, 300, 300};
int[] y1 = {200, 600, 400};

float a, b, c;

void setup() 
  {
  size(600, 800);
  background(0);
  //stroke(255, 255, 0);
  strokeWeight(2);
  
  println(x1.length);
  }

void draw() 
  {
  a = a + random(-TAU/8, TAU/8);   // TAU rad = 360 deg
  println(a);
  stroke(255, 0, 0);
  drawPoints(a, 0);
  
  b = b + random(-TAU/16, TAU/16); // TAU rad = 360 deg
  stroke(255, 255, 0);
  drawPoints(b, 1);
  
  c = c + random(-TAU/32, TAU/8); // TAU rad = 360 deg
  stroke(0, 255, 0);
  drawPoints(c, 2);
  }
  
  
void drawPoints(float ran, int i)
  {     
  float newx = x1[i] + cos(ran) * 5;
  float newy = y1[i] + sin(ran) * 5;
  
  //line(x1[i], y1[i], newx, newy);
  
  strokeWeight(2);
  point(newx, newy);
  
  x1[i] = int(newx);
  y1[i] = int(newy);
  
  if(newx < 0 || newy < 0 || newx > width || newy > height) 
    {
    noLoop();
    }  
  }

:)

1 Like

If you take a closer look they are not exactly the same, but very similar. If you want them to behave distinctly different you need to have separate changing values as @Chrisir and @glv suggests

Dear Chrisir,

Thank you for your help. I makes sense that every cos and sin should also be unique by making an array.
My code was the simple version of what i want to make. So i am going to find out how i can simplify this.

1 Like

Dear glv,

The resources are very welcome. I did try to find an answer on processing.org/reference/, but i didn’t know what i was looking for.
Will save this for future reference.