Project Development
- Fertility & Death Rate Integration: By this phase, I fully incorporated fertility and death rates. Each region now grows or shrinks based on these factors, and in Stability Mode, populations stabilize around a 2.1 replacement rate.
- Mode Switching: Users can toggle between Growth, Random, and Stability modes using the keyboard.
- Visual Feedback: The size of each circle dynamically adjusts based on the population changes, offering a visual representation of how populations are evolving in each region.
- Significant Code Progress: Implemented the full logic for handling different population behaviors across modes.
Code Refinements
In this phase, I added full functionality for fertility and death rates, and populations now stabilize in Stability Mode.
let regions = [];
let mode = 0; // Mode selector
function setup() {
createCanvas(800, 800);
let gridSize = 4;
let spacing = width / gridSize;
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
let x = spacing * i + spacing / 2;
let y = spacing * j + spacing / 2;
let population = random(50, 200);
let fertilityRate = random(1.5, 5);
let deathRate = random(0.5, 3);
regions.push(new Region(x, y, population, fertilityRate, deathRate));
}
}
}
function draw() {
background(255);
switch (mode) {
case 0:
growthMode();
break;
case 1:
randomMode();
break;
case 2:
stabilityMode();
break;
}
}
function keyPressed() {
if (key === '1') mode = 0;
if (key === '2') mode = 1;
if (key === '3') mode = 2;
}
function growthMode() {
for (let region of regions) {
region.grow();
region.display();
}
}
function randomMode() {
for (let region of regions) {
region.randomizeRates();
region.grow();
region.display();
}
}
function stabilityMode() {
for (let region of regions) {
region.stabilize();
region.grow();
region.display();
}
}
Three Variations for Export
- Growth Mode: Populations grow or shrink based on initial fertility and death rates.
- Random Mode: Random changes in fertility and death rates cause erratic population behaviors.
- Stability Mode: Populations gradually stabilize around the replacement rate.
I exported these three variations in SVG format using the save() function in p5.js.
function keyPressed() {
if (key === 's') {
save(); // Saves the current state as SVG
}
}