14 Replies Latest reply on Jul 17, 2007 1:38 PM by kglad

# Is there one group of pixels

Is there a way using bitmap data to tell if all the pixels in a given clip are contiguous? I've got a randomly created shape (not square) and it is possible that there might be two (or more other shapes) drawn. Any tips for how to proceed?
• ###### 1. Re: Is there one group of pixels
so you want to see if the shapes share any pixels?
• ###### 2. Re: Is there one group of pixels
the bitmapdata object is going to be rectangular but your bitmaps are transparent and you want to know if any non-transparent pixels in your bitmap overlap with another bitmaps non-transparent pixels, true?

or you want to know if the non-transparent pixels in one bitmap form a connected set of points?
• ###### 3. Re: Is there one group of pixels
kglads second option. If the non-transparent pixels in one bitmap form a connected set of points.
• ###### 4. Re: Is there one group of pixels
start at one corner of your bitmap and iterate through all pixels forming an array of the non-transparent pixels.

then start forming an another array. pick any pixel from the first array to add to this 2nd array. then look at all 8 of its neighbors and add any of them that are non-transparent pixels in your bitmap. then look at the neighbors of the subset of 8 that are in your 2nd array and add any that are not already added. continue like this until there are no more to add.

now loop through your first array making sure each pixel in it is also in the connected set you added to the 2nd array. if you reach the end of array one without a failure, your bitmap is connected. otherwise, it is not.
• ###### 5. Re: Is there one group of pixels
I don't suppose you have any tips for how to implement that?

I figured make a checkNeighbors function that is send a point and checks its neighbors, if a point is non-transparent and not already in the second array push it onto the second array and call the checkNeighbors function on it. I'm running into the recursive problem.

Another idea I'm having is to use an interval and have it call the checkNeighbor function on any points that have been added to the second array since the last time it was called.

Any other ideas?
• ###### 6. Re: Is there one group of pixels
Well it does work, but it is very slow. I was testing it on a 100 x 100 clip and it took about 90 seconds. Kind of fun, but I need to speed it up. Any ideas?
• ###### 7. Re: Is there one group of pixels
it might be worthwhile to start a 3rd array that stores the latest set of neighbors that qualify to be in the 2nd array (ie, non-trans pixels of the bitmap and not already in the 2nd array). then form a 4th array of the unique 3rd array neighbors that qualify to be 2nd array members. after looping through all the 3rd array members, reinitialize the 3rd array and push the 4th array members into the 2nd array and 3rd array and repeat looping through the 3rd array. i don't think you can loop through more than 8 times the number of pixels in your bitmap.
• ###### 8. Re: Is there one group of pixels
that is very clever.. I tested it out, and it gave me the "There is a script in this movie.." bit.. is this happening for you?
• ###### 9. Re: Is there one group of pixels
No, I'm not getting that message. Because of the setInterval it should run forever. Mine is running for 80 to 90 seconds with no message. How big is your clip? Comment out everything from second array on. Do you still get the message? That first loop is the only part that might take too long. I think? hmmmm.....

Anyway, thanks kglad. This is for a sort of tanagrams kind of thing. Dragging different shapped blocks around on the screen. I was trying this approach because I don't know of any other way to see if the items are all touching. It is easy to control that new shapes are only added if they are touching, but the user could drag tiles from the middle of the shape and create a rift.

I haven't tried the multiple arrays yet. I did speed it up by adding a second frame to the tiles that is just a wire frame. I switch to the second frame when I make my bitmap and then switch back. That way the number of solid pixels goes WAY down. But it is still too slow.

Back to the drawing board! Thanks so much.
• ###### 10. Re: Is there one group of pixels
that is weird, even after comments all that out, i get the message, and if i say not to abort, and let it run, nothing happens, even after 5 minutes.. and i'm using a 100x100 square, all black.
• ###### 11. Re: Is there one group of pixels
why not give them all a 1 pixel transparent border and then use a shape-based hitTest to determine that they are all in contact with one another?
• ###### 12. Re: Is there one group of pixels
i was thinking about hitTest as well, just never thought about using the border.. but it seems like that would be more efficient speed wise.
• ###### 13. Re: Is there one group of pixels
The hitTest (or even better grantSkinner's Collision class) is attractive. I think that the same algorithm using the Collisions might just work. I just have to figure out how to adapt it. hmmmm…
• ###### 14. Re: Is there one group of pixels
are your shapes movieclips and you're using the bitmapdata draw() method to create bitmaps of your movieclips? if so, just a double for-loop and the hitTest() method of bitmaps to check that your movieclips are contiguous.