Coding a Linked List utilizing PVector.Random2D

The above pic is a visualization of ~1000 nodes in a linked list, and here’s a video of me coding it. 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.

1 Like

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?

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

1 Like

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

}
};

1 Like