# Why are my lines moving the same?

**URL:** <https://discourse.processing.org/t/why-are-my-lines-moving-the-same/22392>\
**Category:** Coding Questions\
**Created:** [July 4, 2020, 7:40pm UTC](https://discourse.processing.org/t/why-are-my-lines-moving-the-same/22392 "2020-07-04T19:40:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vreakz](https://avatars.discourse-cdn.com/v4/letter/v/bb73d2/32.png) [@vreakz](https://discourse.processing.org/u/vreakz)\
**Post date:** [July 4, 2020, 7:40pm UTC](https://discourse.processing.org/t/why-are-my-lines-moving-the-same/22392/1 "2020-07-04T19:40:26Z")

</div>

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.

```auto
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();
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 4, 2020, 8:13pm UTC](https://discourse.processing.org/t/why-are-my-lines-moving-the-same/22392/2 "2020-07-04T20:13:47Z")

</div>

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

```auto

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();
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 4, 2020, 9:51pm UTC](https://discourse.processing.org/t/why-are-my-lines-moving-the-same/22392/3 "2020-07-04T21:51:30Z")

</div>

Hello,

> [@vreakz](#):
>
> 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.

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:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

`:)`

Consider using functions…

```auto
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();
    }  
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fc371837571d7ca5a53cf727d62994db2ae02f1d.png)

`:)`

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [July 5, 2020, 9:04am UTC](https://discourse.processing.org/t/why-are-my-lines-moving-the-same/22392/4 "2020-07-05T09:04:30Z")

</div>

> [@vreakz](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![vreakz](https://avatars.discourse-cdn.com/v4/letter/v/bb73d2/32.png) [@vreakz](https://discourse.processing.org/u/vreakz)\
**Post date:** [July 5, 2020, 9:43am UTC](https://discourse.processing.org/t/why-are-my-lines-moving-the-same/22392/5 "2020-07-05T09:43:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vreakz](https://avatars.discourse-cdn.com/v4/letter/v/bb73d2/32.png) [@vreakz](https://discourse.processing.org/u/vreakz)\
**Post date:** [July 5, 2020, 9:48am UTC](https://discourse.processing.org/t/why-are-my-lines-moving-the-same/22392/6 "2020-07-05T09:48:18Z")

</div>

Dear glv,

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