/////////////////////////////////////////////////////////////////////////////// // PredPreySim2.pde - Processing program to simulate predator-prey cycles // (Skeleton with data and starting point for the simulation loop) // Your job: Implement the Lotka-Volterra equations where indicated! // (c) 2013 Marie desJardins /////////////////////////////////////////////////////////////////////////////// // Variable declarations and initializations int prey=80; // initial number of predators int predators=40; // initial number of predators float b_prey=2.0; // birth rate of prey float d_prey=0.001; // death rate of prey float b_pred=0.005; // birth rate of predators float d_pred=0.6; // death rate of predators int time=0; // time starts at zero int max_cycles=200; // maximum number of time steps to simulate int max_pred=5000; // limit on the number pf predators int max_prey=5000; // limit on the number of prey int new_prey; // temp variable for new prey population each cycle int new_predators; // temp variable for new predator population each cycle int height = 500; // height of display region for graph int width = 2000; // width of display region for graph int border = 30; // additional border space on all four sides of graph float graph_x = border + (width * time / max_cycles); // initial x position (for t=0) float prey_graph_y = border + height - (height * prey / max_prey); // initial y position for prey graph float pred_graph_y = border + height - (height * predators / max_pred); // initial y position for predator graph float prev_graph_x; // temp variable for previous x position in graph float prev_pred_graph_y; // temp variable for previous y position of predator line float prev_prey_graph_y; // temp variable for previous y position of prey line color pred_color = color (255, 0, 0); // predator line is red color prey_color = color(0, 0, 255); // prey line is blue PrintWriter output = createWriter ("outfile.csv"); // open file for recording data /////////////////////////////////////////////////////////////////////////////// // Program // Initialize output file and display size (width + 2*border, height + 2*border); // Simulation loop while (time < max_cycles) { time = time+1; // Update number of prey and predators using L-V equations and temp variables new_prey = floor (prey + (b_prey * prey) - (d_prey * prey * predators)); new_predators = floor (predators + (b_pred * predators * prey) - (d_pred * predators)); // Make sure prey and predators are in the range [1, max_prey] new_prey = min (max_prey, max (1, new_prey)); new_predators = min (max_pred, max (1, new_predators)); // Update counts prey = new_prey; predators = new_predators; // Save previous graph positions for line drawing prev_graph_x = graph_x; prev_prey_graph_y = prey_graph_y; prev_pred_graph_y = pred_graph_y; // YOUR CODE GOES BELOW HERE! IT SHOULD COMPUTE GRAPH_X, // PREY_GRAPH_Y, AND PRED_GRAPH_Y, THEN USE PRED_COLOR AND // PREY_COLOR AS FILL COLORS TO DRAW A LINE FOR PREDATORS // AND A LINE FOR PREY }