Gillius's Snowball Algorithm to Combining Dirty Rectangles Introduction: This algorithm combines all rectangles from some "source" (could be a single list or many) into combined rectangles in which no changed area will be covered by multiple rectangles. It may be possible that an unchanged area may be covered by multiple rectangles. The advantage is that flicker and redrawing will be eliminated, without requireing many comparisons and calculations. The disadvantage is that area that does not need to be updated (ie unchanged background) may be written to needlessly. This would be eliminated by splitting pieces so that two colliding triangles become three, although this would be much more complex as the third triangle would have to span across both shared and unshared area between the rectangles. Procedure: Move a rect from the "source" to a new list which can grow to at worst case as many rectangles are in the source (if none collide). Pick a rect from the source and make this the working rectangle. Compare this rectangle to each other, checking for a collision. If there is a collision, combine the two rectangles into one. This new rectangle becomes the working rectangle. Continue in the set -- you do not have to compare to previously compared rectangles. This is where the "snowball" comes from -- as you traverse the list, your working rectangle "sweeps up" other rectanges as it continues through the list. While there are more source rects, go to the previous setup. When you pick an entirely new rect you do start over in the new list. Now you can use these rects and copy them from your buffer to the screen. Performace: The number of comparisons made in the worst case is equal to (x)(x-1)/2 For example, x is the number of source rects and f(x) is the comparisons made: x = 0 1 2 3 4 5 6 f(x) = 0 0 1 3 6 10 15 Suggestions: C/C++/JAVA - The working rectangle can be a pointer. You can set this pointer to a source rect and compare. If you find a rect to combine with, modify the rect that is in the list to contain both rects, and set this rect to your working rect and continue. On a second collision you'll have to remove the old working rect from the list, or you can remove the current rectangle.