xxxxxxxxxx
132
let leftSidePic;
let rightSidePic;
let sourcePics = [];
let sourcePicIndex = 0;
function setup() {
createCanvas(2000, 2000);
//noLoop();
newSample = createImage(2000, 2000);
}
function buildArray(n, fillFunction) {
let outputArray = [];
for (let i = 0; i < n; i++) {
outputArray.push(fillFunction(i));
}
return outputArray;
}
function randomInteger(min, max) {
return Math.floor(min + (max - min) * Math.random());
}
function pick(inputArray) {
return inputArray[randomInteger(0, inputArray.length)];
}
// getting some functions from lesson 07
function randomRange(min, max) {
return min + (max - min) * Math.random();
}
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
move(xDistance, yDistance) {
return new Point(this.x + xDistance, this.y + yDistance);
}
}
function randomPoint(xMin, xMax, yMin, yMax) {
return new Point(randomRange(xMin, xMax), randomRange(yMin, yMax));
}
let sampleLocation = new Point(0, 0);
function createMatrix(nRows, nColumns, elemFunc) {
let outputMatrix = [];
for (let yi = 0; yi < nRows; yi++) {
let row = [];
for (let xi = 0; xi < nColumns; xi++) {
row.push(elemFunc(yi, xi));
}
outputMatrix.push(row);
}
return outputMatrix;
}
let gridSize = 20;
let pointGrid = createMatrix(gridSize, gridSize, (r, c) => {
return new Point(r * 100, c * 100);
});
console.log(pointGrid);
let samplingPoints = buildArray(gridSize ** 2, (i) =>
randomPoint(0, 650, 0, 930)
);
function preload() {
img = loadImage("voi-small.jpg");
sourcePics[0] = loadImage("flowers-pink-1-23-edit-1.jpg");
sourcePics[1] = loadImage("flowers-pink-2-23-edit-1.jpg");
sourcePics[2] = loadImage("flowers-purple-23-edit-1.jpg");
sourcePics[3] = loadImage("flowers-yellow-23-edit-1.jpg");
}
//let eyeCoordinates = buildArray (30,i => new Point(20 + (i* 100), 100))
function draw() {
background(0);
pointGrid.forEach((r, i) => {
r.forEach((c) => {
if (mouseX > c.x && mouseX < c.x + 80) {
if (Math.random() < 0.5) {
newSample.copy(
sourcePics[sourcePicIndex],
Math.floor(sampleLocation.x) + Math.floor(Math.random() * 40),
Math.floor(sampleLocation.y) + Math.floor(Math.random() * 40),
320,
320,
c.x,
c.y,
80,
80
);
} else {
newSample.copy(
sourcePics[sourcePicIndex],
Math.floor(randomRange(0, sourcePics[0].width)),
Math.floor(randomRange(0, sourcePics[0].height)),
320,
320,
c.x,
c.y,
80,
80
);
}
}
});
});
image(newSample, 0, 0);
fill("white");
textSize(20);
text("xPosition: " + mouseX, 30, 50);
text("yPosition: " + mouseY, 30, 80);
}
function mouseClicked() {
sourcePicIndex = randomInteger(1, 4);
sampleLocation = randomPoint(
0,
0,
sourcePics[sourcePicIndex].width - 360,
sourcePics[sourcePicIndex].height - 360
);
}