Mod:Coding Challenge #6: Mitosis Simulation with p5.js
Mitosisは有糸分裂、細胞分裂のことっぽい。マウスでクリックするとセル(細胞)が分裂するというもの。p5.jsで書いているのを観てProcessingで書くのをリアルタイムでやってみるけど、意外とフムフムと考えながらやれるのは良い。それがprocessing.jsで動くように最後上げ直すのとかちょっと頭使うけど、訓練にはなる。
int n = 30; ArrayList<Cell> cells = new ArrayList<Cell>(); // setup関数 : 初回1度だけ実行される void setup() { size(960, 540, P2D); // ウィンドウサイズを960px,540pxに colorMode(HSB, 360, 100, 100, 100); // HSBでの色指定にする for (int i = 0; i < 30; i++) { cells.add(new Cell()); } } // draw関数 : setup関数実行後繰り返し実行される void draw() { background(220, 20, 10); for (Cell cell : cells) { cell.move(); cell.draw(); } } void mousePressed() { ArrayList<Cell> removeCells = new ArrayList<Cell>(); ArrayList<Cell> addCells = new ArrayList<Cell>(); Cell removeCell = new Cell(); for (Cell cell : cells) { if (cell.isClicked(mouseX, mouseY)) { addCells.addAll(cell.mitosis()); removeCells.add(cell); removeCell = cell; } } cells.remove(removeCell); cells.addAll(addCells); //cells.removeAll(removeCells); } class Cell { PVector pos; float r; color c; Cell() { pos = new PVector(random(width), random(height)); r = 50; c = color(random(360), random(80, 100), random(80, 100), 60); } Cell(PVector _pos, float _r, color _c) { pos = _pos; r = _r; c = _c; } void move() { PVector vel = PVector.random2D(); pos.add(vel); } void draw() { noStroke(); fill(c); ellipse(pos.x, pos.y, r*2, r*2); } boolean isClicked(float x, float y) { float dist = PVector.dist(pos, new PVector(x, y)); if (dist < r) { return true; } else { return false; } } ArrayList<Cell> mitosis() { ArrayList<Cell> addCell = new ArrayList<Cell>(); Cell cellA = new Cell(new PVector(pos.x-r/2, pos.y), r/2, c); Cell cellB = new Cell(new PVector(pos.x+r/2, pos.y), r/2, c); Cell cellC = new Cell(new PVector(pos.x, pos.y-r/2), r/2, c); Cell cellD = new Cell(new PVector(pos.x, pos.y+r/2), r/2,c); addCell.add(cellA); addCell.add(cellB); addCell.add(cellC); addCell.add(cellD); return addCell; } Cell copy() { return new Cell(pos, r, c); } }