c++ - How to use the BFS algorithm to keep only the outer points? -
let's have 500x500 2d grid (from -250 250).
each cell of grid has value, 0 100.
what i've been trying keep list of cell value lower 50 starting @ point (0, 0), not keep points, outer points (perimeter, bounds, cell value lower 50 not have 4 adjacent cells value lower 50).
the way implemented algorithm, keep list of points. how should modify it? optimize code if possible (i can have bigger grids), prefer not iterate again.
pointlist pointswithvaluebelow50; // list of points value below 50 map<point, bool> pointdiscovered; // map of points passed through point = {0,0}; point b; int value; queue q; q.enqueue(a); pointdiscovered[a] = true; while(!q.isempty()) { = q.dequeue(a) value = calculatevalue(a); // random method verify if points has value below 50 in grid if (value < 50) { pointswithvaluebelow50.add(a); b[0] = a[0]+1; b[1] = a[1]; if(pointdiscovered[b] == false) { q.enqueue(b) pointdiscovered[b] = true; } b[0] = a[0]-1; b[1] = a[1]; if(pointdiscovered[b] == false) { q.enqueue(b) pointdiscovered[b] = true; } b[0] = a[0]; b[1] = a[1]+1; if(pointdiscovered[b] == false) { q.enqueue(b) pointdiscovered[b] = true; } b[0] = a[0]; b[1] = a[1]-1; if(pointdiscovered[b] == false) { q.enqueue(b) pointdiscovered[b] = true; } } }
as can see, keep in list points value below 50, instead of outer points. how can modify code? should use algorithm?
let consider cell array grey-scale image (where cell values pixel intensities). attempting ("find borders") common problem in graphics, , there well-known algorithm (marching squares) find such borders lists (with additional property of lists being ordered, when traverse list, going around vertexes of polygon contains interesting area.
marching squares o(cells) (it needs @ each cell-neighbourhood once in worst case), , can parallelized greater performance. there many variations, based on considered relevant transition , whether want find one boundary or all boundaries.
there many implementations out there. 1 of cleanest have read here (lgpl; returns boundaries; configurable threshold) - translating java c++ should relatively straightforward.
Comments
Post a Comment