xxxxxxxxxx
102
// Title: Coding Challenge #6 - Mitosis
// Author: Daniel Kaye
// URL: http://dnkaye.com/2020_CodingChallenges.html
//
// Based on Daniel Schiffman's (Coding Train) coding challenges
// URL: https://thecodingtrain.com/CodingChallenges/006-mitosis-p5.html
//
// What is this?
// An attempt to simulate cell mitosis, in some sense
//
// How do I do this?
// I attempt to complete Daniel Schiffman's coding challenges before watching
// his video, then afterwards I watch and see what he did differently, and learn!
// Things I'm proud of / differences from The Coding Train version:
// -using polar coordiantes (angle) to set and change directions of travel of particles (cells)
// -mitosis triggered by max size OR mouse press
// -reversal of direction at screen edges using trig
// -shrinking animation of cell during mitosis, sorta looks like they're actually splittling off from eachother
let cells = []; // array to hold all cells
let speed = 0.25; // appro speed at which cells move
let growthRate = 0.05 // approx growth rate for cells
let fullyGrownRadius = 25; // max size a cell will grow too before splitting
let maxNumOfCells = 1000; // program resets when there are more than 1000 cells (prevents creashing)
function setup() {
createCanvas(400, 400);
cells.push(new Cell(width / 2, height / 2, fullyGrownRadius * 0.75)); // creates first cell
}
function draw() {
if (focused || frameCount < 30) {
background(255);
drawCells(); // draws, moves, and grows all cells
// resets cells if there are too many (prevents crashing, sometimes)
if (cells.length > maxNumOfCells) {
resetCells();
}
drawNumOfCells(); // writes current number of cells to the screen
} else {
drawUnpauseInstructions();
}
}
function drawUnpauseInstructions(){
noStroke();
fill(0);
textAlign(CENTER);
textSize(18);
text('click to activate', width/2, height - height/5);
}
// draws all cells in array
function drawCells() {
for (let cell of cells) { // for all cells....
cell.grow(); // incrimentally grows each cell
cell.move(); //incrimentally moves each cell
cell.draw(); // draws cell to screen
// cell starts mitosis if it's reached it's fully grown size
if (cell.r >= fullyGrownRadius) { // if a cell has reaced it's maximum size....
mitosis(cell); // split that cell
}
}
}
function mousePressed() {
// if you click on a cell, start mitosis
for (let i = 0; i < cells.length; i++) {
if (cells[i].isHovered()) {
mitosis(cells[i]);
print('mitosis! ' + cells.length);
i = cells.length;
}
}
}
// makes a cell split in two
function mitosis(cell) {
cell.splitting = true;
let newDir = cell.dir + PI; // new direction = opposite direction of cell it's splitting off of
newDir += (random(0, PI / 2)); // slightly alters, randomizes new cell direction to make sketch appear slightly mroe organic
if (newDir > 2 * PI) {
newDir -= 2 * PI;
}
cells.push(new Cell(cell.x, cell.y, cell.r * 0.5));
cells[cells.length - 1].dir = newDir;
}
// clears array of cells, starts a new array with only one
function resetCells() {
cells = [];
cells.push(new Cell(width / 2, height / 2, fullyGrownRadius * 0.75)); // creates first cell
}
function drawNumOfCells() {
noStroke();
fill(192);
textSize(14);
textAlign(LEFT);
text('number of cells: ' + cells.length, 10, height - 10);
}