chunkyorkshireman
YaBB Newbies
Offline
Posts: 40
Diffuse
Nov 18th , 2009, 8:54am
Hi. I have written this diffuse sketch which seems to work fine visually but when i print the value of the centre cell to check its working against my maths it doesn't tally - have checked and checked again my maths and sure correct so i can't figure what might be wrong with the code. Can anyone offer any advice please. Thanks. Basically the centre cell is setup with a value of 100, everything else is empty. Diffusion is at 0.9. So, the value in centre cell should go: 100, 10, 11.125, etc... (thats the hand maths) but the sketch works out: 100, 10, 9.02..... also when checking the centre cells neighbours they go 0, 11.25 (which correlates to my maths) but then again at third step goes 'wrong'. Also i am sure that the values for the corner cells should be equal and the 4 (north, east, south west also) as they have same topology to centre cell - but each cells value is different - which means something must be wrong with the code. the diffusion function is: each cell gives away .9 of its value and takes 1/8th of the diffused value of each of its 8 neighbours. thankyou for any help! float diffRate = 0.9; Cell [][] grid; Cell [][] Limbo; int numberOfColours = 255; color[]colours = new color[numberOfColours]; /*........................................................................*/ void setup() { size(200,200); colorMode(HSB, 50); for(int i=0; i<numberOfColours; i++){ colours[i] = color((numberOfColours-i), i, 255); } grid = new Cell[width][height]; Limbo = new Cell[width][height]; for (int i=0; i<width; i++ ) { for (int j=0; j<height; j++ ) { // Initialize each object grid[i][j] = new Cell(i,j,0); Limbo[i][j] = new Cell(i,j,0); } } grid[width/2][height/2].pH = 100; //test: setup centre cell with pheromone } /*......................................................................*/ void draw() { frameRate(125); println(grid[width/2][height/2].pH); //check value of test cell diffuse(); for (int i=0; i<width; i++ ) { for (int j=0; j<height; j++ ) { grid[i][j].render(); } } } void diffuse() { for(int i=0; i<width; i++){ for(int j=0; j<height; j++){ float neighsPhero = 0; float phero = grid[i][j].pH; //addup neighbours pheromone values neighsPhero = (((grid[((i-1) +width) %width] [((j-1) +height) %height].pH *diffRate) /8) + //Northwest ((grid[(i +width) %width] [((j-1) +height) %height].pH *diffRate) /8) + //North ((grid[((i+1) +width) %width] [((j-1) +height) %height].pH *diffRate) /8) + //Northeast ((grid[((i+1) +width) %width] [(j +height) %height].pH *diffRate) /8) + //East ((grid[((i+1) +width) %width] [((j+1) +height) %height].pH *diffRate) /8) + //Souteast ((grid[(i +width) %width] [((j+1) +height) %height].pH *diffRate) /8) + //South ((grid[((i-1) +width) %width] [((j+1) +height) %height].pH *diffRate) /8) + //Southwest ((grid[((i-1) +width) %width] [(j +height) %height].pH *diffRate) /8)) ; //West phero = (phero - (phero*diffRate)) //amount distributed to neighbours + neighsPhero; //amount taken from neighbours Limbo[i][j].pH = phero; } } grid = Limbo; } class Cell { int x,y; // x,y location float pH; //pheromone value /*...................................................................*/ Cell(int xpos, int ypos, float pheromone) { x = xpos; y = ypos; pH = pheromone; } /*....................................................................*/ void render() { //colour the pheromone int phero = floor(grid[x][y].pH); //translate .pH to an int (rounded down) to allow colour scale stroke(colours[phero]); point(x, y); } }