Advertisement
Guest User

grass fire

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. //http://what-when-how.com/introduction-to-video-and-image-processing/blob-analysis-introduction-to-video-and-image-processing-part-1/
  2.  
  3. void burn(int x, int y, int w, int h, int[] image, boolean[] visited, boolean onFire, ArrayList<ArrayList<Integer>> result) {
  4.   int index = y*w+x;
  5.  
  6.   //if (y < 0 || y >= h || x < 0 || x >= w)
  7.   //  return;
  8.   if(y >= 0 && y < h && x >= 0 && x < w && !visited[index]) {
  9.     visited[index] = true;
  10.  
  11.     if (image[index] == 1) {
  12.       if(!onFire)
  13.         result.add(new ArrayList<Integer>());
  14.       result.get(result.size()-1).add(index);
  15.    
  16.       burn(x+1, y, w, h, image, visited, true, result);
  17.       burn(x, y+1, w, h, image, visited, true, result);
  18.       burn(x-1, y, w, h, image, visited, true, result);
  19.       burn(x, y-1, w, h, image, visited, true, result);
  20.     }
  21.   }
  22.   if(!onFire && index+1 < image.length) {
  23.     index++;
  24.     burn(index%w,index/w, w, h, image, visited, false, result);
  25.   }
  26. }
  27.  
  28. int[] _image = {
  29.   0, 0, 1, 1, 1,
  30.   0, 0, 0, 1, 0,
  31.   0, 0, 0, 1, 0,
  32.   0, 1, 1, 0, 0,
  33.   0, 1, 1, 0, 0,
  34.   0, 1, 1, 0, 0
  35. };
  36. ArrayList<ArrayList<Integer>> _result;
  37. boolean[] _visited;
  38. color[] _rcolor;
  39.  
  40. void setup() {
  41.   size(500, 600);
  42.  
  43.   _result = new ArrayList<ArrayList<Integer>>();
  44.  
  45.   _visited = new boolean[_image.length];
  46.   for (int i=0; i<_visited.length; i++)
  47.     _visited[i] = false;
  48.  
  49.   _rcolor = new color[100];
  50.   for (int i=0; i<_rcolor.length; i++)
  51.     _rcolor[i] = color((int)random(256),(int)random(256),(int)random(256));
  52.  
  53.   burn(0, 0, 5, 6, _image, _visited, false, _result);
  54.   println(_result.size());
  55. }
  56.  
  57. void draw() {
  58.   for (int x=0; x<5; x++) {
  59.     for (int y=0; y<6; y++) {
  60.       int index = y*5+x;
  61.       color c = (_image[index] == 1) ? color(255, 255, 255) : color(127, 127, 127);
  62.       for(int i=0; i<_result.size(); i++) {
  63.         for(int j=0; j<_result.get(i).size(); j++) {
  64.           if(index == _result.get(i).get(j)) {
  65.             c = _rcolor[i];
  66.             break;
  67.           }
  68.         }
  69.       }
  70.       fill(c);
  71.       rect(x*100, y*100, 100, 100);
  72.     }
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement