xxxxxxxxxx
108
// Daniel Shiffman
// http://youtube.com/thecodingtrain
// http://codingtra.in
// Recaman Sequence
// https://youtu.be/DhFZfzOvNTU
let numbers = [];
let count = 1;
let sequence = [];
let index = 0;
let scl = 0;
let arcs = [];
let biggest = 0;
let p;
const percentHistory = [];
class Arc {
constructor(start, end, dir) {
this.start = start;
this.end = end;
this.dir = dir;
}
show() {
let diameter = abs(this.end - this.start);
let x = (this.end + this.start) / 2;
stroke(255);
strokeWeight(0.5);
noFill();
if (this.dir == 0) {
arc(x, 0, diameter, diameter, PI, 0);
} else {
arc(x, 0, diameter, diameter, 0, PI);
}
}
}
function setup() {
createCanvas(windowWidth, windowHeight - 50);
frameRate(30);
background(0);
numbers[index] = true;
sequence.push(index);
p = createP();
}
function step() {
let next = index - count;
if (next < 0 || numbers[next]) {
next = index + count;
}
numbers[next] = true;
sequence.push(next);
let a = new Arc(index, next, count % 2);
arcs.push(a);
index = next;
if (index > biggest) {
biggest = index;
}
count++;
}
function draw() {
for (let i = 0; i < 1000; i++) {
step();
}
//translate(0, height / 2);
//scl = lerp(scl, width / biggest, 0.1);
//scale(scl);
background(0);
if (percentHistory.length > width) {
translate(count - width, 0);
}
for (let a of arcs) {
//a.show();
}
const fraction = sequence.length / biggest;
const percent = fraction * 100;
percentHistory.push(percent);
let x = 0;
let maximum = -Infinity;
stroke(255);
noFill();
beginShape();
for (const percent of percentHistory) {
const y = map(percent, 0, 100, 0, height);
vertex(x, y);
if (percent > maximum) {
maximum = percent;
}
x += 0.5;
}
endShape();
p.html(`${round(percent)}% of number line covered. Max: ${round(maximum)}%`)
}