tags:

views:

160

answers:

2

I was wondering how drawing applications draw the entire time the mouse is down without having empty gaps. What I mean is, for example if the program only drew circles at the mouse's X, y coordinate, then if the mouse went too quicly it would seem like a bunch of little circles rather than a nice continuous line. How can this be done without constantly drawing a short straight line between where the mouse was 0.001 seconds ago and where the mouse now is. Thanks

+2  A: 

You can draw a short curved line between where the mouse was 0.001 seconds ago and where the mouse now is.

The mouse positions are not continuous, if you move it faster than very slowly, you will get discrete unconnected points. You need to interpolate between these points using your preferred method.

Jens Björnhager
Okay so the way I do it using a timer and drawing short straight lines is correct?
Milo
You could use a Bézier curve http://en.wikipedia.org/wiki/B%C3%A9zier_curve to some good effect.
Nissan Fan
You don't need a timer. You just need to record the previous mouse location, then draw a line to the current mouse location. If you're doing fancier interpolation (say, cubic) you just need to keep track of more points in the past.
Ron Warholic
@user146780: Listen for the mouse move event if possible
Cameron
@Nissan Fan: A better curve would be a cubic hermite spline like Catmull-Rom or a cardinal spline. This way you can directly use the previously sampled points instead of offsetting them in order to get the tangents to line up.
Ron Warholic
+3  A: 

It can't be done without constantly drawing a line between the current mouse point and the previous point, which is why this is what drawing programs generally do do.

Fancier drawing programs will fit curvy lines to multiple previous points to achieve a more natural drawing stroke, but the principle is the same.

Update: Based on a comment, it appears that you have a timer involved in your drawing code. This is surely unnecessary, since your application will generate a MouseMove event whenever the mouse is moved at all, and you can use that event to draw the next line.

MusiGenesis
Line, or, some sort of [curve](http://en.wikipedia.org/wiki/B%C3%A9zier_curve)
BlueRaja - Danny Pflughoeft
"Bezier" = "fit curvy lines to multiple points". :)
MusiGenesis
Would it be a good idea to create arrays of these as instructions to generate lines, then push these into a queue for each operation then just pop them off for an undo?
Milo
@user146780: well, yeah, but you're basically describing what any modern graphics library does. I don't know what's available in the C++ world, but there must be a lot of choices to go with, as opposed to rolling your own.
MusiGenesis
Ok thanks! I'd rather do it on my own rather than use a lib because I want to learn and understand the underlying code.
Milo
@user146: If you are talking about undo/redo support, you'd want to undo the entire blob of drawing created using a single mouse click, not the individual lines drawn between frames
BlueRaja - Danny Pflughoeft