Structs.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package main.java.com.lotkavolterra;
  2. import processing.core.PApplet;
  3. import processing.core.PImage;
  4. public class Structs extends PApplet {
  5. int n_boxes = 30;
  6. int FOX = 0;
  7. int RABBIT = 1;
  8. int GRASS = 2;
  9. int DESERT = 3;
  10. int ROAD = 4;
  11. int N_AGENTS = 4;
  12. PImage[] images = new PImage[5];
  13. int SIZE = -1;
  14. boolean DIAGONAL = false;
  15. int nn = n_boxes *n_boxes;
  16. Box [] boxes = new Box[nn];
  17. Neighbours [] neighs = new Neighbours[nn] ;
  18. class Box {
  19. float x;
  20. float y;
  21. float xend;
  22. float yend;
  23. float w=SIZE;
  24. float h=SIZE;
  25. int type;
  26. int next;
  27. // constr
  28. Box ( float x, float y, int type, int size) {
  29. this.x=x;
  30. this.y=y;
  31. this.w = size;
  32. this.h = size;
  33. this.xend=x+size;
  34. this.yend=y+size;
  35. this.type =type;
  36. this.next =type;
  37. } // constr
  38. Box ( float x, float y, int type) {
  39. this.x=x;
  40. this.y=y;
  41. this.type =type;
  42. this.next =type;
  43. this.xend=x+this.w;
  44. this.yend=y+this.h;
  45. } // constr
  46. void make_borders() {
  47. int j = type;
  48. if (j==GRASS) {stroke(77,137,99);}
  49. else if (j==FOX) {stroke(182,22,63);}
  50. else if (j==DESERT) {stroke(244,148,0);}
  51. else if (j==RABBIT) {stroke(4,54,84);}
  52. strokeWeight(10);
  53. rect(x, y, w, h);
  54. }
  55. void display(int n) {
  56. image(images[n], x, y);
  57. }
  58. } // class
  59. class Neighbours {
  60. int x;
  61. int y;
  62. int [] surround;
  63. Neighbours( int x, int y) {
  64. this.x = x;
  65. this.y = y;
  66. if (DIAGONAL) {
  67. int N = 8;
  68. int[] xs= new int[N];
  69. int[] ys= new int[N];
  70. ys[0] = (n_boxes + y-1)%n_boxes;
  71. ys[1] = ys[0];
  72. ys[2] = ys[0];
  73. ys[3] = y; ys[4] = y;
  74. ys[5] = (y+1)%n_boxes;
  75. ys[6] = ys[5];
  76. ys[7] = ys[5];
  77. xs[0] = (n_boxes + x-1)%n_boxes;
  78. xs[1] = x;
  79. xs[2] = (x+1) %n_boxes;
  80. xs[3] = xs[0]; xs[4] =xs[2];
  81. xs[5] = xs[0];
  82. xs[6] = x;
  83. xs[7] = xs[2];
  84. this.surround = new int[N];
  85. for (int i=0; i<N; i++){
  86. surround[i] = xs[i]+ys[i]*n_boxes;
  87. }
  88. }
  89. else{
  90. int N = 4;
  91. int[] xs= new int[N];
  92. int[] ys= new int[N];
  93. this.surround = new int[N];
  94. ys[0] = (n_boxes + y-1)%n_boxes;
  95. ys[1] = y;
  96. ys[2] = y;
  97. ys[3] = (y+1)%n_boxes;
  98. xs[0] = x;
  99. xs[1] = (n_boxes + x-1)%n_boxes;
  100. xs[2] = (x+1) %n_boxes;
  101. xs[3] = x;
  102. for (int i=0; i<N; i++){
  103. surround[i] = xs[i]+(ys[i]*n_boxes);
  104. }
  105. }
  106. }
  107. }
  108. }