views:

195

answers:

2

You're making a drawing program like Paint. You want to be able to undo/redo brush strokes. How would you implement this?

Optimize for speed and memory.

A: 

Create a backup copy of the canvas. Choose a rectangular patch that completely surrounds the brush stroke. Save the bitmap contained in that patch in both the new version and the backup. You can now blit these changes to undo or redo the stroke.

May use a lot of memory.

Nick Retallack
Breaking up a long stroke into a series of short strokes should go a long way toward reducing the needed memory. Think about drawing a 'U' shape and using a set of boxes following the U-shaped path vs. one bounding box covering the entire 'U'
MadCoder
+2  A: 

Use a quadtree to record the previous state of the part of the canvas that changed. On an undo, replace the canvas state from the quadtree.

Ira Baxter