4

I'd like to pick your brains on how to implement freeform drawing in a paint app. Given that the command object would encompass the click down, the arbitrarily dragging around, and the release, how would this be necessarily stored in the command and drawn onto the bitmap context?

For starters, would the data simply be every pixel coordinate reported in the mousemove command put in a large list? I can't think of any other obvious approach since the user is probably not drawing long completely straight lines that could be optimized.

Would the drawing of it be to essentially stamp solid circles (of the radius that is the pen width) at every coordinate reported in the mouse move, and interpolated in between if the move jumps far enough?

Update: Clarification on what I meant when I asked how the data would be stored. I was talking about how the data in the command object would look and figured it would be a list of 'move-to' pixel coordinates to represent the action. I wasn't referring to the representation of the data in the bitmap image that was being drawn upon.

Joey
  • 7,457
  • 12
  • 50
  • 103
  • The comments you make are how I would do it (on mouse down, start to draw, for each pixel moved, draw a radius=PenWidth circle, continue to do so till on mouse up). – jcolebrand Oct 15 '10 at 22:57
  • As for how to store it, it's a bitmap ... it's like a large 2D array and each cell is the color value that you want to manipulate. So you would update each cell's colorvalue. – jcolebrand Oct 15 '10 at 22:59
  • Why don't you look at the source code of gimp/paint.net e.t.c?? – kazanaki Oct 18 '10 at 11:17

1 Answers1

0

I guess it depends on whether you want to have undo functionality. If not, you don't need to store the command list, you can just update the bitmap in the MouseMove handler.

If you want to be able to undo, then you might want to store the commands (in which case a list of coordinates that the mouse moved to during drawing i.e. while the mouse button is down) is one way to do it. You would also need to keep track of the settings (e.g. pen radius, colour, etc.).

Alternatively you could just store multiple copies of the bitmap after each command finished (although this would use a lot of memory for large bitmaps).

Jackson Pope
  • 13,897
  • 6
  • 52
  • 80
  • This seems to work pretty well. I drew a circle with the pen radius at the first point, then for each subsequent mousemove point, I drew a line to that point followed by a circle at that point to round the corners. My plan is to track the penradius and color changes as separate actions, but perhaps that means down the line my undo should move past these non-pixel changing actions when popping the stack of actions. – Joey Oct 18 '10 at 16:40
  • As an afterthought, I think that that is how Photoshop/GIMP does it, to store a full copy of the image (or maybe it's a diff somehow) of the total image from each revision to the next. I'm not entirely sure, however. – jcolebrand Dec 26 '10 at 15:02