xxxxxxxxxx
131
let entities = [];
let entityWidth = 2;
let buf1 = [];
let buf2 = [];
let PARTITION = 0;
let MERGE = 1;
let state = PARTITION;
let partSize = 0;
let idx = 0;
let startPartition = 0;
let b1idx = 0;
let b2idx = 0;
let wait = 0;
let record = false;
let startRecording = true;
let stopRecording = false;
let fps = 60;
let capturer;
function setup() {
createCanvas(400, 400);
for(let i = 0; i < width/entityWidth;i++)
entities.push(random(height));
frameRate(fps);
colorMode(HSB, height, 100, 100);
if (record){
capturer = new CCapture({
format: 'png',
framerate: fps
});
}
}
function draw() {
if (record && startRecording){
capturer.start();
startRecording = false;
}
if (wait > 0){
wait -= deltaTime;
if (record){
capturer.capture(document.getElementById('defaultCanvas0'));
}
return;
}
if (record && stopRecording){
capturer.stop();
capturer.save();
noLoop();
}
background(20);
for(let i = 0, x = 0; i < entities.length; x+=entityWidth, i++){
let c = color(entities[i], 70, 80);
fill(c);
stroke(c);
rect(x, height, entityWidth, -entities[i]);
}
let done = true;
for(let i = 1; i < entities.length; i++){
if (entities[i-1] > entities[i]){
done = false;
break;
}
}
if (done){
entities = []
for(let i = 0; i < width/entityWidth;i++)
entities.push(random(height));
state = PARTITION;
partSize = 0;
idx = 0;
startPartition = 0;
b1idx = 0;
b2idx = 0;
wait = 1000;
buf1 = [];
buf2 = [];
stopRecording = true;
}
if (b1idx >= buf1.length && b2idx >= buf2.length){
//idx = 0;
state = PARTITION;
if (partSize == 0 || idx >= entities.length){
if (partSize == 0)
partSize = 1;
else
partSize *= 2;
idx = 0;
}
}
if (state == PARTITION){
startPartition = idx;
buf1 = [];
buf2 = [];
b1idx = 0;
b2idx = 0;
for(let i = 0; idx < entities.length && i < partSize; i++){
buf1.push(entities[idx++]);
}
for(let i = 0; idx < entities.length && i < partSize; i++){
buf2.push(entities[idx++]);
}
state = MERGE;
idx = startPartition;
}
if (b1idx < buf1.length && b2idx < buf2.length){
if (buf1[b1idx] < buf2[b2idx]){
entities[idx++] = buf1[b1idx++];
}
else {
entities[idx++] = buf2[b2idx++];
}
}
else if (b1idx < buf1.length){
entities[idx++] = buf1[b1idx++];
}
else{
entities[idx++] = buf2[b2idx++];
}
if (record){
capturer.capture(document.getElementById('defaultCanvas0'));
}
}