How to get a rectangle that encloses all drawn shapes ?

Jan 18, 2011 at 9:45 AM
Edited Jan 18, 2011 at 1:33 PM

Using ink I can do something like Strokes.Getbounds() that returns a rectangle that encloses all the ink strokes.  I am trying to figure out the best way to do this with shapes drawn on a writeable bitmap.  I have thought about iterating through all the pixels and comparing the coordinates until I arrive at the points that will enclose everything but this does not seem a very efficient method.  Any suggestions or hints welcome.

Coordinator
Jan 18, 2011 at 2:17 PM

If you fill the WriteableBitmap with shapes or other things, you can keep track of the object's size and constantly increase the bounding rectangle.

 If you have a prefilled bitmap and want to get it's bounding rectangle, you have to iterate through the pixels. However, you can do it the smart way. ;) Split the detection into 4 steps. Start to find the top coordinate by beginning the loop at y=0 (outer loop) and x=0 (inner loop). When a colored pixel is found take the y coordinate and you have the top coordinate of the bounding rectangle (t). Then start at x=0 outer loop and y=t inner loop to search from the left side. The x coordinate of the first pixel is the left position of the bounding rectangle. Then do a similar search for the right and bottom coordinates by starting at the width / height and using the previous found bounding coordinates as end condition.

In the worst case you have to search not less pixels than with the brute force naïv approach, but in the most cases you'll probably find the bounding rectangle faster with this approach I described.

Just an idea.

 

- Rene Schulte