First of all, sorry for the confusing title. My sketch will make it clearer.
I have written a function that makes faces that are composed of seperate but overlayed facial elements (which are png’s):
-head shape
-nose
-eyebrows
-eyes
-mouth
-hair
There are 10 of each, so basically with this sketch we can make 10 million faces and I want to find twins. If two faces in a grid of faces are exactly the same, I want to highlight those faces (e.g. if i compare 2 faces, like in the sketch below, i want the BG color to become red if they’re twins).
But I’m breaking my head over how to do this, because I can’t find a way to compare the 2 instances of my drawFace() function. Scoping issue, of course. But I’m too new to P5js to fully wrap my head around this problem.
This is the code of the complete sketch. Maybe it makes more sense to take a look at the full screen version with the PNG’s: p5.js Web Editor
Thanks for the help!
let brow = [];
let eyes = [];
let hair = [];
let head = [];
let mouth = [];
let nose = [];
let names = [];
let lastnames = [];
let firstname;
let lastname;
let cols = 2;
let rows = 1;
//number of variations per facial element
let variations = 10;
function preload() {
//Preload all variations of facial elements
for (let i = 1; i <= variations; i++) {
brow[i] = loadImage("img/brow" + i + ".png");
eyes[i] = loadImage("img/eyes" + i + ".png");
hair[i] = loadImage("img/hair" + i + ".png");
head[i] = loadImage("img/head" + i + ".png");
mouth[i] = loadImage("img/mouth" + i + ".png");
nose[i] = loadImage("img/nose" + i + ".png");
//preload JSON file met 1000 last names en 1000 voornamen
lastnames = loadJSON("lastnames.json");
firstnames = loadJSON("names.json");
}
}
function setup() {
createCanvas(1080, 1080);
background(220);
}
function draw() {
background(230);
for (let x = 0; x < width; x += width / cols) {
for (let y = 0; y < height; y += height / rows) {
drawFace(x, y + height / (cols * 2), width / cols, width / cols);
}
}
frameRate(1);
}
function drawFace(x, y, w, h) {
let firstname;
let lastname;
let brownr = int(random(1, variations + 1));
let eyesnr = int(random(1, variations + 1));
let hairnr = int(random(1, variations + 1));
let headnr = int(random(1, variations + 1));
let mouthnr = int(random(1, variations + 1));
let nosenr = int(random(1, variations + 1));
image(brow[brownr], x, y, w, h);
image(eyes[eyesnr], x, y, w, h);
image(hair[hairnr], x, y, w, h);
image(head[headnr], x, y, w, h);
image(mouth[mouthnr], x, y, w, h);
image(nose[nosenr], x, y, w, h);
textAlign(CENTER);
firstname = firstnames.data[int(random(0, 1000))];
lastname = lastnames.data[int(random(0, 1000))];
text(str(firstname + " " + lastname), x + w / 2, y + h + 20);
}
function keyPressed() {
if (key === " ") {
let code;
code = split(str(millis()), ",");
let title;
title = "Head Nr " + code;
saveCanvas(title, "png");
}
}