Graphs.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package main.java.com.lotkavolterra;
  2. import processing.core.PApplet;
  3. import processing.core.PConstants;
  4. import processing.core.PImage;
  5. public class Graphs extends Structs {
  6. public int WIDTH = 1900;
  7. public int HEIGHT = 1000;
  8. public PImage[] images_g = new PImage[5];
  9. public Structs.Box[] colors = new Structs.Box[5];
  10. public int PIC_SIZE = 80;
  11. public int TRACES = 40;
  12. public int[] counter = new int[5];
  13. public int[][] traces = new int[N_AGENTS][TRACES];
  14. 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
  15. String[] myNames = {"fox", "rabbit", "grass", "desert"}; // picture names,, add more
  16. public void set_traces(Densities d){
  17. for (int i = 0; i < TRACES; i += 1) {
  18. traces[FOX][i] = (int) d.foxes;
  19. }
  20. for (int i = 0; i < TRACES; i += 1) {
  21. traces[RABBIT][i] = (int) d.rabbits;
  22. }
  23. for (int i = 0; i < TRACES; i += 1) {
  24. traces[GRASS][i] = (int) d.grass;
  25. }
  26. for (int i = 0; i < TRACES; i += 1) {
  27. traces[DESERT][i] = (int) d.desert;
  28. }
  29. }
  30. public void load_image(){
  31. PImage im ;
  32. PImage im_g;
  33. for (int x=0; x < 5; x++ ) {
  34. im = loadImage(myFileArray[x]);
  35. im.resize(SIZE, SIZE);
  36. images[x] = im;
  37. }
  38. for (int x=0; x < 5; x++ ) {
  39. im_g = loadImage(myFileArray[x]);
  40. im_g.resize(PIC_SIZE, PIC_SIZE);
  41. images_g[x] = im_g;
  42. }
  43. }
  44. // count the number of agents from the boxes
  45. public void count_boxes(){
  46. for (int n=0; n<5; n++) {
  47. counter[n] = 0;
  48. }
  49. for (int i = 0; i < nn; i++) {
  50. counter[boxes[i].type] +=1;
  51. }
  52. }
  53. // Visualizations: update_traces creates the fading effect on the plots
  54. public void update_traces(){
  55. count_boxes();
  56. for (int j = 0; j < 4; j += 1) {
  57. if (TRACES - 1 >= 0) {
  58. System.arraycopy(traces[j], 1, traces[j], 0, TRACES - 1);
  59. traces[j][TRACES-1] = counter[j];
  60. } else {
  61. traces[j][TRACES-1] = counter[j];
  62. }
  63. }
  64. }
  65. public void arrow(int x1, int y1, int x2, int y2) {
  66. strokeWeight(3);
  67. line(x1, y1, x2, y2);
  68. pushMatrix();
  69. translate(x2, y2);
  70. float a = atan2(x1-x2, y2-y1);
  71. rotate(a);
  72. strokeWeight(3);
  73. line(0, 0, -10, -10);
  74. line(0, 0, 10, -10);
  75. popMatrix();
  76. }
  77. public void draw_box(Bar bar) {
  78. fill(218);
  79. stroke(141);
  80. rect(bar.x,bar.y, bar.w, bar.h, 10);
  81. fill(0);
  82. rect(bar.x,bar.y, bar.my_fill, bar.h, 10);
  83. }
  84. public void draw_button(Button button) {
  85. fill(218);
  86. stroke(141);
  87. rect(button.x, button.y, button.w, button.h, 10);
  88. textAlign(CENTER, CENTER);
  89. fill(0);
  90. text(button.label, button.x + (button.w / 2), button.y + (button.h / 2));
  91. }
  92. public void draw_slider(Slider slider) {
  93. stroke(0);
  94. line(slider.x, slider.y, slider.x+ slider.w, slider.y );
  95. circle(slider.w0+slider.x, slider.y, PIC_SIZE/2);
  96. textAlign(CENTER, CENTER);
  97. fill(0);
  98. text(slider.label, slider.x + (slider.w / 2), slider.y + (PIC_SIZE / 2));
  99. }
  100. // Make the plots on the right side
  101. public void plot_traces() {
  102. float mean;
  103. strokeWeight(3);
  104. textSize(30);
  105. textAlign(LEFT);
  106. stroke(182,22,63);
  107. // Upper plot with phase space
  108. text("Volpi", WIDTH/2 + 2*PIC_SIZE, 2*PIC_SIZE );
  109. arrow(WIDTH/2 + 2* PIC_SIZE, HEIGHT/2, WIDTH/2 + 2*PIC_SIZE, 2*PIC_SIZE);
  110. stroke(4,54,84);
  111. arrow(WIDTH/2 + 2* PIC_SIZE, HEIGHT/2, WIDTH *3/4, HEIGHT/2);
  112. text("Conigli", WIDTH*3/4, HEIGHT/2 );
  113. for (int i = 0; i < TRACES-1; i += 1) {
  114. stroke(0, i*255/TRACES);
  115. if (traces[RABBIT][i] > 0) {
  116. line(WIDTH/2+ 2*PIC_SIZE +WIDTH/2 *traces[RABBIT][i]/nn,
  117. HEIGHT/2 *(nn-traces[FOX][i])/nn,
  118. WIDTH/2+ 2*PIC_SIZE + WIDTH/2 *traces[RABBIT][i+1]/nn ,
  119. HEIGHT/2*(nn-traces[FOX][i + 1])/nn);
  120. }
  121. }
  122. // Lower plot with pop densities
  123. for (int j = 0; j < 4; j += 1) {
  124. mean = 0;
  125. for (int i = 0; i < TRACES-1; i += 1) {
  126. mean += traces[j][i];
  127. }
  128. mean /= TRACES;
  129. if (j==GRASS) {stroke(77,137,99);}
  130. else if (j==FOX) {stroke(182,22,63);}
  131. else if (j==DESERT) {stroke(244,148,0);}
  132. else if (j==RABBIT) {stroke(4,54,84);}
  133. strokeWeight((float) 2.5);
  134. for (int i = 0; i < TRACES-1; i += 1) {
  135. if (traces[j][i] > 0) {
  136. line(HEIGHT + 200 +i*15,
  137. HEIGHT/2 +PIC_SIZE +(10 * PIC_SIZE * (traces[j][i] )/nn),
  138. HEIGHT + 200 +i*15+15,
  139. HEIGHT/2+ +PIC_SIZE +(10 * PIC_SIZE * (traces[j][i + 1])/nn));
  140. }
  141. }
  142. }
  143. }
  144. // Initialization
  145. }