123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package main.java.com.lotkavolterra;
- import processing.core.PApplet;
- import processing.core.PConstants;
- import processing.core.PImage;
- public class Graphs extends Structs {
- public int WIDTH = 1900;
- public int HEIGHT = 1000;
- public PImage[] images_g = new PImage[5];
- public Structs.Box[] colors = new Structs.Box[5];
- public int PIC_SIZE = 80;
- public int TRACES = 40;
- public int[] counter = new int[5];
- public int[][] traces = new int[N_AGENTS][TRACES];
- String[] myFileArray = {"main/resources/pictures/fox.jpg", "main/resources/pictures/rabbit.jpg", "main/resources/pictures/grass.jpeg", "main/resources/pictures/soil.jpg", "main/resources/pictures/road.jpeg"}; // picture names,, add more
- String[] myNames = {"fox", "rabbit", "grass", "desert"}; // picture names,, add more
- public void set_traces(Densities d){
- for (int i = 0; i < TRACES; i += 1) {
- traces[FOX][i] = (int) d.foxes;
- }
- for (int i = 0; i < TRACES; i += 1) {
- traces[RABBIT][i] = (int) d.rabbits;
- }
- for (int i = 0; i < TRACES; i += 1) {
- traces[GRASS][i] = (int) d.grass;
- }
- for (int i = 0; i < TRACES; i += 1) {
- traces[DESERT][i] = (int) d.desert;
- }
- }
- public void load_image(){
- PImage im ;
- PImage im_g;
- for (int x=0; x < 5; x++ ) {
- im = loadImage(myFileArray[x]);
- im.resize(SIZE, SIZE);
- images[x] = im;
- }
- for (int x=0; x < 5; x++ ) {
- im_g = loadImage(myFileArray[x]);
- im_g.resize(PIC_SIZE, PIC_SIZE);
- images_g[x] = im_g;
- }
- }
- // count the number of agents from the boxes
- public void count_boxes(){
- for (int n=0; n<5; n++) {
- counter[n] = 0;
- }
- for (int i = 0; i < nn; i++) {
- counter[boxes[i].type] +=1;
- }
- }
- // Visualizations: update_traces creates the fading effect on the plots
- public void update_traces(){
- count_boxes();
- for (int j = 0; j < 4; j += 1) {
- if (TRACES - 1 >= 0) {
- System.arraycopy(traces[j], 1, traces[j], 0, TRACES - 1);
- traces[j][TRACES-1] = counter[j];
- } else {
- traces[j][TRACES-1] = counter[j];
- }
- }
- }
- public void arrow(int x1, int y1, int x2, int y2) {
- strokeWeight(3);
- line(x1, y1, x2, y2);
- pushMatrix();
- translate(x2, y2);
- float a = atan2(x1-x2, y2-y1);
- rotate(a);
- strokeWeight(3);
- line(0, 0, -10, -10);
- line(0, 0, 10, -10);
- popMatrix();
- }
- public void draw_box(Bar bar) {
- fill(218);
- stroke(141);
- rect(bar.x,bar.y, bar.w, bar.h, 10);
- fill(0);
- rect(bar.x,bar.y, bar.my_fill, bar.h, 10);
- }
- public void draw_button(Button button) {
- fill(218);
- stroke(141);
- rect(button.x, button.y, button.w, button.h, 10);
- textAlign(CENTER, CENTER);
- fill(0);
- text(button.label, button.x + (button.w / 2), button.y + (button.h / 2));
- }
- public void draw_slider(Slider slider) {
- stroke(0);
- line(slider.x, slider.y, slider.x+ slider.w, slider.y );
- circle(slider.w0+slider.x, slider.y, PIC_SIZE/2);
- textAlign(CENTER, CENTER);
- fill(0);
- text(slider.label, slider.x + (slider.w / 2), slider.y + (PIC_SIZE / 2));
- }
- // Make the plots on the right side
- public void plot_traces() {
- float mean;
- strokeWeight(3);
- textSize(30);
- textAlign(LEFT);
- stroke(182,22,63);
- // Upper plot with phase space
- text("Volpi", WIDTH/2 + 2*PIC_SIZE, 2*PIC_SIZE );
- arrow(WIDTH/2 + 2* PIC_SIZE, HEIGHT/2, WIDTH/2 + 2*PIC_SIZE, 2*PIC_SIZE);
- stroke(4,54,84);
- arrow(WIDTH/2 + 2* PIC_SIZE, HEIGHT/2, WIDTH *3/4, HEIGHT/2);
- text("Conigli", WIDTH*3/4, HEIGHT/2 );
- for (int i = 0; i < TRACES-1; i += 1) {
- stroke(0, i*255/TRACES);
- if (traces[RABBIT][i] > 0) {
- line(WIDTH/2+ 2*PIC_SIZE +WIDTH/2 *traces[RABBIT][i]/nn,
- HEIGHT/2 *(nn-traces[FOX][i])/nn,
- WIDTH/2+ 2*PIC_SIZE + WIDTH/2 *traces[RABBIT][i+1]/nn ,
- HEIGHT/2*(nn-traces[FOX][i + 1])/nn);
- }
- }
- // Lower plot with pop densities
- for (int j = 0; j < 4; j += 1) {
- mean = 0;
- for (int i = 0; i < TRACES-1; i += 1) {
- mean += traces[j][i];
- }
- mean /= TRACES;
- if (j==GRASS) {stroke(77,137,99);}
- else if (j==FOX) {stroke(182,22,63);}
- else if (j==DESERT) {stroke(244,148,0);}
- else if (j==RABBIT) {stroke(4,54,84);}
- strokeWeight((float) 2.5);
- for (int i = 0; i < TRACES-1; i += 1) {
- if (traces[j][i] > 0) {
- line(HEIGHT + 200 +i*15,
- HEIGHT/2 +PIC_SIZE +(10 * PIC_SIZE * (traces[j][i] )/nn),
- HEIGHT + 200 +i*15+15,
- HEIGHT/2+ +PIC_SIZE +(10 * PIC_SIZE * (traces[j][i + 1])/nn));
- }
- }
- }
- }
- // Initialization
- }
|