# Coding a Linked List utilizing PVector.Random2D

**URL:** <https://discourse.processing.org/t/coding-a-linked-list-utilizing-pvector-random2d/11998>\
**Category:** Gallery\
**Created:** [June 11, 2019, 10:58pm UTC](https://discourse.processing.org/t/coding-a-linked-list-utilizing-pvector-random2d/11998 "2019-06-11T22:58:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zooey105](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zooey105/32/5535_2.png) [@zooey105](https://discourse.processing.org/u/zooey105)\
**Post date:** [June 11, 2019, 10:58pm UTC](https://discourse.processing.org/t/coding-a-linked-list-utilizing-pvector-random2d/11998/1 "2019-06-11T22:58:33Z")

</div>

![001030](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6ab49dcc9955deb9791aad81277b83bf317e8dab.gif)

The above pic is a visualization of ~1000 nodes in a linked list, and here’s a [video of me coding it](https://www.youtube.com/watch?v=07KldH07ji4&feature=youtu.be). Would anyone be interested in a series of data-structure heavy videos? Dan from the Coding Train does a lot of white-paper focused videos but he’s far more talkative than I.

---

<div class="post-metadata">

**Author:** ![zenemig](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zenemig/32/4418_2.png) [@zenemig](https://discourse.processing.org/u/zenemig)\
**Post date:** [June 13, 2019, 4:21pm UTC](https://discourse.processing.org/t/coding-a-linked-list-utilizing-pvector-random2d/11998/2 "2019-06-13T16:21:39Z")

</div>

Hi @zooey105.  
Very interesting results. In the description of the video you mention the source code but is not there, is there a way yo look at it?

---

<div class="post-metadata">

**Author:** ![zooey105](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zooey105/32/5535_2.png) [@zooey105](https://discourse.processing.org/u/zooey105)\
**Post date:** [June 13, 2019, 8:47pm UTC](https://discourse.processing.org/t/coding-a-linked-list-utilizing-pvector-random2d/11998/3 "2019-06-13T20:47:27Z")

</div>

Yes!! Here it is, I’m planning on doing a few more linked-list videos:

import peasy._;  
import peasy.org.apache.commons.math._;  
import peasy.org.apache.commons.math.geometry.\*;  
import java.util.Date;

Node h1, h2, h3, h4;  
//PeasyCam cam;  
int amt = 3000;

int c = 0;  
void setup() {  
size(1000, 1000);  
//size(1600, 900);  
//fullScreen();  
//cam = new PeasyCam(this, 100);  
//pixelDensity(displayDensity());  
colorMode(HSB);

float s = width/4;

h1 = new Node(new PVector(width/2 - s, height/2 - s));  
h2 = new Node(new PVector(width/2 - s, height/2 + s));  
h3 = new Node(new PVector(width/2 + s, height/2 - s));  
h4 = new Node(new PVector(width/2 + s, height/2 + s));

//for (int i = 0; i \< amt; i++) {  
// head.addNext();  
//}

background(41);  
}

void draw() {  
h1.display();  
h1.jitter();  
h1.addNext();

h2.display();  
h2.jitter();  
h2.addNext();

h3.display();  
h3.jitter();  
h3.addNext();

h3.display();  
h3.jitter();  
h3.addNext();

c++;  
if (c \> 3600) {  
saveFrame(“fin-4/######.gif”);  
exit();  
}  
}

void keyPressed() {  
if (key == ’ ') {  
for (int i = 0; i \< amt; i++) {  
// what’s your vector, victor?  
h1.addNext(new Node(new PVector(random(-width, width), random(-height, height))));  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![zooey105](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zooey105/32/5535_2.png) [@zooey105](https://discourse.processing.org/u/zooey105)\
**Post date:** [June 13, 2019, 8:48pm UTC](https://discourse.processing.org/t/coding-a-linked-list-utilizing-pvector-random2d/11998/4 "2019-06-13T20:48:44Z")

</div>

Here’s the node code:

class Node {  
PVector pos;  
int count = 0;

Node next;

Node(PVector \_pos) {  
pos = \_pos.copy();  
next = null;  
}

void addNext() {  
count++;  
if (next == null) {  
PVector next\_pos = pos.copy().add(PVector.random2D().mult(25));  
if (next\_pos.x \< 0) {  
next\_pos.x += width;  
}  
if (next\_pos.y \< 0) {  
next\_pos.y += height;  
}  
next = new Node(next\_pos);  
} else {  
next.addNext();  
}  
}

void addNext(Node n) {  
count++;  
if (next == null) {  
next = n;  
} else {  
next.addNext(n);  
}  
}

void display(PVector prev) {  
stroke(255, 255, 255);  
strokeWeight(0.7);  
//line(prev.x, prev.y, prev.z, pos.x, pos.y, pos.z);  
line(prev.x, prev.y, pos.x, pos.y);  
display();  
}

void jitter() {  
pos.add(PVector.random2D());  
if (next != null) next.jitter();  
}

void display() {

```
strokeWeight(3.5);
stroke((this.count/2.0 + 50)%255, 255, 255, 25);

// modulo width/height to wrap on canvas
point(pos.x%width, pos.y%height);

if (next != null) {
  strokeWeight(0.7);
  stroke((this.count/1.0 + 50)%255, 255, 255, 5);
  line(next.pos.x%width, next.pos.y%height, pos.x%width, pos.y%height);
  next.display();
}

```

}  
};
