Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://what-when-how.com/introduction-to-video-and-image-processing/blob-analysis-introduction-to-video-and-image-processing-part-1/
- void burn(int x, int y, int w, int h, int[] image, boolean[] visited, boolean onFire, ArrayList<ArrayList<Integer>> result) {
- int index = y*w+x;
- //if (y < 0 || y >= h || x < 0 || x >= w)
- // return;
- if(y >= 0 && y < h && x >= 0 && x < w && !visited[index]) {
- visited[index] = true;
- if (image[index] == 1) {
- if(!onFire)
- result.add(new ArrayList<Integer>());
- result.get(result.size()-1).add(index);
- burn(x+1, y, w, h, image, visited, true, result);
- burn(x, y+1, w, h, image, visited, true, result);
- burn(x-1, y, w, h, image, visited, true, result);
- burn(x, y-1, w, h, image, visited, true, result);
- }
- }
- if(!onFire && index+1 < image.length) {
- index++;
- burn(index%w,index/w, w, h, image, visited, false, result);
- }
- }
- int[] _image = {
- 0, 0, 1, 1, 1,
- 0, 0, 0, 1, 0,
- 0, 0, 0, 1, 0,
- 0, 1, 1, 0, 0,
- 0, 1, 1, 0, 0,
- 0, 1, 1, 0, 0
- };
- ArrayList<ArrayList<Integer>> _result;
- boolean[] _visited;
- color[] _rcolor;
- void setup() {
- size(500, 600);
- _result = new ArrayList<ArrayList<Integer>>();
- _visited = new boolean[_image.length];
- for (int i=0; i<_visited.length; i++)
- _visited[i] = false;
- _rcolor = new color[100];
- for (int i=0; i<_rcolor.length; i++)
- _rcolor[i] = color((int)random(256),(int)random(256),(int)random(256));
- burn(0, 0, 5, 6, _image, _visited, false, _result);
- println(_result.size());
- }
- void draw() {
- for (int x=0; x<5; x++) {
- for (int y=0; y<6; y++) {
- int index = y*5+x;
- color c = (_image[index] == 1) ? color(255, 255, 255) : color(127, 127, 127);
- for(int i=0; i<_result.size(); i++) {
- for(int j=0; j<_result.get(i).size(); j++) {
- if(index == _result.get(i).get(j)) {
- c = _rcolor[i];
- break;
- }
- }
- }
- fill(c);
- rect(x*100, y*100, 100, 100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement