xxxxxxxxxx
62
let padovan = [];
function setup() {
createCanvas(600, 600); // Adjusted width for better visibility
// Generate the Padovan sequence up to P(12) to match the 12 rows
for (let i = 0; i < 12; i++) {
padovan[i] = padovanNumber(i);
}
// Shuffle the Padovan sequence
shuffle(padovan, true);
noLoop(); // Draw only once
}
function draw() {
background(255);
//let rectHeight = 40; // Fixed height for each rectangle
let spacing = 2.9; // Space between rectangles in the same row
let fixedRowSpacing = 41; // Fixed space between rows
let totalWidth = 50 * (2 + spacing) - spacing; // Total width of the barcode
let startX = (width - totalWidth) / 2; // Calculate starting X position to center
for (let i = 0; i < 12; i++) { // 12 rows
let yPosition = i * fixedRowSpacing + 150; // Calculate Y position for each row
let rectHeight = random(20,50);
// Introduce a horizontal shift based on Padovan number
let rowShift = padovan[i] < 7 ? (i * random(-2, 2)) : 5; // Shift only if Padovan number > 9
let spacing = random(1.5 , 10);
for (let j = 0; j < 21; j++) { // 21 rectangles in each row
// Use the shuffled Padovan number for width
let rectWidth = padovan[i] < 5 ? (i *random(0.5 , 1.5)) : 2;
fill(0); // Set fill color to black
// Draw rectangle, applying the row shift
rect(startX + rowShift + j * (rectWidth + spacing), yPosition - rectHeight, rectWidth, rectHeight);
}
}
}
// Function to calculate Padovan number
function padovanNumber(n) {
if (n === 0 || n === 1 || n === 2) {
return 1;
} else {
return padovanNumber(n - 2) + padovanNumber(n - 3);
}
}
// Function to shuffle an array
function shuffle(array, shuffleInPlace = false) {
if (!shuffleInPlace) {
array = array.slice(); // Create a copy if not shuffling in place
}
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
}
return array;
}