tags:

views:

21

answers:

3

Hello! I have a prototype of a simple drawing application. When the user drags a finger across the screen, I record the points along the way and draw a series of lines between them. In other words, a drawing is a list of “paths” and each path is a list of points to connect. This is easy, it works and it’s efficient.

The problem is I’d like to implement an eraser tool. In a regular bitmap editor the eraser simply erases pixels, but in my drawing there are no pixels to erase – all pixels are created dynamically by stroking the paths. I could do a simple eraser by “drawing” using the background colour, overlaying the already painted paths. But I’d like to draw on a textured background, so that’s a no-go.

How would you do this? (Short of the obvious solution of representing the drawing as a bitmap where the eraser is simple.)

+2  A: 

You can't implement an eraser in the traditional sense; what you describe with recording the paths and drawing them dynamically is vector graphics. The concept of an eraser comes from raster graphics (a bitmap, basically). With vector graphics, the user generally selects an item or an area of items to delete.

If you really wanted to do this, you'd basically have to do collision detection between all of the paths in your graphic and the rectangle (or whatever shape) of the eraser. When contact occurs, you'd have to cut the colliding graphic object on either side of the eraser by using the slope of the line(s) in contact with the eraser and the point of intersection.

Adam Robinson
+1  A: 

You could probably find the intersections of your existing paths and the deleted area, split the existing paths up, and create new points at the intersections (which would become start/end points of the newly split paths).

Grant Crofton
Sorry, I just realised this is much what @Adam Robinson says in the latter half of his reply.
Grant Crofton
+1  A: 

I could do a simple eraser by “drawing” using the background colour, overlaying the already painted paths. But I’d like to draw on a textured background, so that’s a no-go.

Can't you do an "eraser by drawing" except you don't use a single color but the whole background as color. I mean, for a given path to erase, you take one by one each pixel and color it with the background color of the same pixel cordinates

Sebastien Lorber