UNPKG

cacatoo

Version:

Building, exploring, and sharing spatially structured models

130 lines (102 loc) 4.98 kB
<html> <script src="../../dist/cacatoo.js"></script> <!-- Include cacatoo library (compiled with rollup) --> <script src="../../lib/all.js"></script> <!-- Load other packages --> <link rel="shortcut icon" type="image/jpg" href="../../patterns/cacatoo.png"/> <link rel="stylesheet" href="../../style/cacatoo.css"> <!-- Set style sheet --> <script> /*-----------------------Start user-defined code ---------------------*/ let sim; var draw_cells = 1 var range_helping = 50 var strength_helping = 0.02 var range_competition = 10 var strength_competition = 0.0005 function cacatoo() { let simconfig = { title: "Turing patterns in population growth", // The name of your cacatoo-simulation description: "", // And a description if you wish maxtime: 10000, // How many time steps the model continues to run // (note, the onscreen FPS may drop below 60 fps when using fast mode, although many more timesteps may be handled per second) ncol: 200, // Number of columns (width of your grid) nrow: 200, // Number of rows (height of your grid) scale: 2, // Scale of the grid (nxn pixels per grid point) sleep: 0, wrap: [false,false], fpsmeter: true } // FLOCKCONFIG EXAMPLE // This example sets up a boid simulation with specific values for the currently implemented parameters // Note however, all these parameters have defaults, so not all need to be set by the user. let flockconfig = { // Flock parameters num_boids: 0, // Starting number of boids (flocking individuals) shape: 'dot', // Shape of the boids drawn (options: bird, arrow, line, rect, dot, ant) click: 'kill', // Clicking the boids pushes them away from the mouse max_speed: 1, // Maximum velocity of boids max_force: 0.2, // Maximum steering force applied to boids (separation/cohesion/alignment rules) init_velocity:0, friction: 0.5, // Them ants are darn slippery :) brownian: 0.1, // Mouse parameters mouse_radius: 30, // Radius of boids captured by the mouse overlay draw_mouse_radius: 'true', // Show a circle where the mouse is draw_mouse_colour: 'blue', // Collision behaviour collision_force: 0.5, size: 3, // Size of the boids (scales drawing and colision detection) // Optimalisation (speed) parameters qt_visible: false, // Show quadtree (optimalisation by automatically tessalating the space) qt_capacity: 3, // How many boids can be in one subspace of the quadtree before it is divided further } sim = new Simulation(simconfig) // Initialise the Cacatoo simulation sim.makeFlockmodel("flock", flockconfig) // Add a flockmodel, which contains invidiuals (boids) in continuous space sim.reset = function(){ sim.flock.boids = [] sim.flock.populateSpot(10,sim.nr/2,sim.nc/2,10) for(let boid of sim.flock.boids) boid.fill="blue" } sim.reset() sim.createFlockDisplay("flock", {legend: true, legendlabel: "internal resources", strokeStyle: "white", strokeWidth: 1, minval:0, maxval:100, num_colours: 200, nticks:3, decimals:0}) sim.flock.update = function(){ for(let boid of this.boids) { if(this.boids.length <= 1000) { let numhelpers = this.getIndividualsInRange(boid.position, range_helping).length let birthrate = numhelpers*strength_helping if(sim.rng.random() < birthrate){ let newboid = this.copyBoid(boid) let angle = sim.rng.random()*Math.PI*2 newboid.position.x+= 0.5*boid.size*Math.cos(angle) newboid.position.y+= 0.5*boid.size*Math.sin(angle) if(this.inBounds(newboid)) this.boids.push(newboid) } } let numcomp = this.getIndividualsInRange(boid.position, range_competition).length let deathrate = numcomp*strength_competition if(sim.rng.random() < deathrate){ const index = this.boids.indexOf(boid); this.boids.splice(index,1) } } if(sim.time %50 == 0) console.log(`Simulation step ${sim.time} has ${sim.flock.boids.length} cells`) } sim.start() sim.addButton("Restart", function () { sim.reset() }) sim.addSlider("range_helping",2,50,1,"Range helping") sim.addSlider("range_competition",2,50,1,"Range killing") } </script> <body onload="cacatoo()"> <div class="header" id="header"> <h2>Cacatoo </h2> </div> <div class="content" id="canvas_holder"></div> <div class="content" id="form_holder"></div> <div class="content" id="graph_holder"> </div> <div class="footer" id="footer"></div> </body> </html>