xxxxxxxxxx
75
// @AminAhmadAhmadi
// Coding Challenge 6: Mitosis Simulation
// https://youtu.be/jxGS3fKPKJA
function Cell(pos, r, c) {
if (pos) {
this.pos = pos.copy();
} else {
this.pos = createVector(width/2, height/2);
}
this.r = r || 60;
this.c = c || color(random(100, 255), 0, random(100, 255), 100);
this.clicked = function(x, y) {
var d = dist(this.pos.x, this.pos.y, x, y);
if (d < this.r) {
return true;
} else {
return false;
}
}
this.mitosis = function() {
this.pos.x += random(-this.r, this.r);
this.pos.y += random(-this.r, this.r);
var cell = new Cell(this.pos, this.r * 0.8, this.c);
return cell;
}
this.move = function() {
var vel = p5.Vector.random2D();
this.pos.add(vel);
if (this.pos.x<this.r){
this.pos.x=this.r;
}
if (this.pos.y<this.r){
this.pos.y=this.r;
}
if (this.pos.x>width-this.r){
this.pos.x=width-this.r;
}
if (this.pos.y>height-this.r){
this.pos.y=height-this.r;
}
}
this.show = function() {
noStroke();
fill(this.c);
ellipse(this.pos.x, this.pos.y, 2 * this.r, 2 * this.r)
}
this.maketangent = (other) => {
let limit = 0;
while (this.overlap(other)) {
if (limit > 1000) {
break;
}
if (this.r < other.r) {
this.pos.x += random(-1, +1);
this.pos.y += random(-1, +1);
} else {
other.pos.x += random(-1, +1);
other.pos.y += random(-1, +1);
}
}
}
this.overlap = (other) => {
let d = dist(this.pos.x, this.pos.y, other.pos.x, other.pos.y);
return (d < this.r + other.r);
}
}