const pens = [{name: "white", clr: null, vec: null}, {name: "black", clr: null, vec: null}, {name: "cyan", clr: null, vec: null}, {name: "magenta", clr: null, vec: null}, {name: "orange", clr: null, vec: null},{name: "darkred", clr: null, vec: null}, ];
img = loadImage("https://upload.wikimedia.org/wikipedia/commons/6/62/Panthera_tigris_sumatran_subspecies.jpg");
img = loadImage("https://upload.wikimedia.org/wikipedia/commons/0/0e/Berlinerfernsehturmdetail.jpg");
img = loadImage("https://upload.wikimedia.org/wikipedia/commons/4/4b/Wachten_op_hoog_water_%28beeld_van_Jan_Ketelaar%29_14-08-2023._%28d.j.b%29_05.jpg")
createCanvas(windowWidth, windowHeight);
if (img.width > img.height) {
for (let i = 0; i < pens.length; i++) {
if (typeof pens[i].name === 'string' || pens[i] instanceof String) color(pens[i].clr = color(pens[i].name));
pens[i].vec = createVector(red(pens[i].clr), green(pens[i].clr), blue(pens[i].clr));
for (let i = 0; i < pens.length; i++) svgGrps[i] = [];
clines = []; makeCLines(img);
translate(width / 2, height / 2);
for (let i = 0; i < clines.length; i++) {
cline.setAngle([0, 0, 0]);
for (let i = 0; i < cubes.length; i++) {
cube.setAngle([i+angle, i+angle/2, i+angle/3]);
function makeCLines(img) {
for (let x = 0; x < img.width; x++) {
for (let y = 0; y < img.height; y++) {
let p_r = img.pixels[idx + 0];
let p_g = img.pixels[idx + 1];
let p_b = img.pixels[idx + 2];
let p_h = map(p_r + p_g + p_b, 0, 768, -6, -5);
pts.push([x-img.width/2, y-img.height/2, p_h]);
clrs.push([p_r, p_g, p_b]);
pns.push(findShortestPen(pens, p_r, p_g, p_b));
const newCLine = new CLine(pts, clrs, pns);
function findShortestPen(pens, r, g, b) {
let clrVec = createVector(r, g, b);
let shortestDist = Infinity;
for (let i = 0; i < pens.length; i++) {
const dst = p5.Vector.dist(clrVec, pen.vec);
if (dst < shortestDist) {
cubes.push(new Cube([-p, -p, -p]));
cubes.push(new Cube([0, -p, -p]));
cubes.push(new Cube([p, -p, -p]));
cubes.push(new Cube([-p, 0, -p]));
cubes.push(new Cube([0, 0, -p]));
cubes.push(new Cube([p, 0, -p]));
cubes.push(new Cube([-p, p, -p]));
cubes.push(new Cube([0, p, -p]));
cubes.push(new Cube([p, p, -p]));
if (key != "s" && key != "S") return;
let svgStr = `<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">\n`;
for (let i = 0; i < svgGrps.length; i++) {
svgStr += `<g fill="none" stroke="${pens[i].name}" stroke-width="0.5">\n`;
const lines = svgGrps[i];
svgStr += getSvgLine(ln.x1, ln.y1, ln.x2, ln.y2, 1);
save(svgStr.split("\n"), "epi_pic.svg.txt");
function getSvgLine(x1, y1, x2, y2, prec = 0) {
return ` <line x1="${nf(x1,0,prec)}" y1="${nf(y1,0,prec)}" x2="${nf(x2,0,prec)}" y2="${nf(y2,0,prec)}" />\n`;