12

I want to write a paint program in the style of MS Paint.

For painting things on screen when the user moves the mouse, I have to wait for mouse move events and draw on the screen whenever I receive one. Apparently, mose move events are not sent very often, so I have to interpolate the mouse movement by drawing a line between the current mouse position and the previous one. In pseudocode, this looks something like this:

var positionOld = null

def handleMouseMove(positionNew):
    if mouse.button.down:
        if positionOld == null:
            positionOld = positionNew
        screen.draw.line(positionOld,positionNew)
        positionOld = positionNew

Now my question: interpolating with straight line segments looks too jagged for my taste, can you recommend a better interpolation method? What method do GIMP or Adobe Photoshop implement?

Alternatively, is there a way to increase the frequency of the mouse move events that I receive? The GUI framework I'm using is wxWidgets.

GUI framework: wxWidgets.
(Programming language: Haskell, but that's irrelevant here)

EDIT: Clarification: I want something that looks smoother than straight line segments, see the picture (original size):

jagged lines drawn between mouse positions

EDIT2: The code I'm using looks like this:

-- create bitmap and derive drawing context
im      <- imageCreateSized (sy 800 600)
bitmap  <- bitmapCreateFromImage im (-1)    -- wxBitmap
dc      <- memoryDCCreate                   -- wxMemoryDC
memoryDCSelectObject dc bitmap

...
-- handle mouse move
onMouse ... sw (MouseLeftDrag posNew _) = do
    ...
    line dc posOld posNew [color     := white
                          , penJoin  := JoinRound
                          , penWidth := 2]
    repaint sw                              -- a wxScrolledWindow

-- handle paint event
onPaint ... = do
    ...
    -- draw bitmap on the wxScrolledWindow
    drawBitmap dc_sw bitmap pointZero False []

which might make a difference. Maybe my choices of wx-classes is why I'm getting a rather low frequency of mouse move events.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Heinrich Apfelmus
  • 11,034
  • 1
  • 39
  • 67
  • 2
    I have no clue if this is possible, but did you consider running a timer that checks the mouse position? Then you would have control over the frequency of the messages. – Frank Schwieterman Jul 27 '10 at 20:06

6 Answers6

4

Live demos

enter image description here

The way to go is 

Spline interpolation of the points

The solution is to store coordinates of the points and then perform spline interpolation.

I took the solution demonstrated here and modified it. They computed the spline after you stop drawing. I modified the code so that it draws immediately. You might see though that the spline is changing during the drawing. For real application, you probably will need two canvases - one with the old drawings and the other with just the current drawing, that will change constantly until your mouse stops.

Version 1 uses spline simplification - deletes points that are close to the line - which results in smoother splines but produce less "stable" result. Version 2 uses all points on the line and produces much more stable solution though (and computationally less expensive).

Tomas
  • 57,621
  • 49
  • 238
  • 373
2

You can make them really smooth using splines: http://freespace.virgin.net/hugo.elias/graphics/x_bezier.htm

But you'll have to delay the drawing of each line segment until one frame later, so that you have the start and end points, plus the next and previous points available for the calculation.

Rocketmagnet
  • 5,656
  • 8
  • 36
  • 47
  • 1
    Nice link about splines, but it only defines splines in terms of two points and two directions. Do you have details on how to create a spline segment given three or four points on the spline? After all, all I'm receiving is a list of mouse positions. – Heinrich Apfelmus Aug 05 '10 at 19:34
  • Sure, you just define the direction for each point to be parallel to the line between the previous and next points. For the first and last points in the sequence, just set the directions to be the zero vector. – Rocketmagnet Aug 26 '10 at 11:32
  • 1
    @HeinrichApfelmus is right, this is not a good idea. You are not able to figure out those directions without some clever statistics evaluating the neighbouring points. – Tomas Jan 19 '14 at 19:37
  • @Tomas - There is nothing clever needed. Those The direction vector for point N is simply parallel to the line between point N-1 and N+1. – Rocketmagnet Jan 27 '14 at 12:02
  • 1
    Aha, you call deriving direction from line *"simply"* - I would like to see your "simple" code then... Anyway, you kind of forgot that the line is what you are supposed to create, it is not your input :-) – Tomas Jan 27 '14 at 13:08
  • @Tomas - The points are your input. A *straight* line between two points is simple to construct. The tangent line for point n is *simply* the line parallel to the straight line from point n-1 to the point n+1. It's utterly *utterly* trivial. – Rocketmagnet Jan 31 '14 at 16:02
  • 1
    Where to find the referred article on beziers? (Link is dead.) – john_who_is_doe Jan 06 '18 at 23:30
2

so, as I see the problem of jagged edge of freehand made curve, when the mouse are moved very fast, is not solved !!! In my opinion there are need to work around with the polling frequency of mousemove event in the system i.e. using different mouse driver or smf.. And the second way is the math.. using some kind of algorithm, to accuratly bend the straight line between two points when the mouse event is polled out.. For clear view you can compare how is drawed free hand line in photoshop and how in mspaint.. thanks folks.. ;)

harviz
  • 21
  • 2
1

I agree with harviz - the problem isn't solved. It should be solved on the operating system level by recording mouse movements in a priority thread, but no operating system I know of does that. However, the app developer can also work around this operating system limitation by interpolating better than linear.

Since mouse movement events don't always come fast enough, linear interpolation isn't always enough.

I experimented a little bit with the spline idea brought up by Rocketmagnet.

Instead of putting a line between two points A and D, look at the point P preceding A and use a cubic spline with the following control points B = A + v' and C = D - w', where

v = A - P,
w = D - A,
w' = w / 4 and
v' = v * |w| / |v| / 4.

This means we fall into the second point with the same angle as the line interpolation would, but go out a starting point in the same angle the previous segment came in, making the edge smooth. We use the length of the segment for both control point distances to make the size of the bend fit its proportion.

The following picture shows the result with very few data points (indicated in grey).

enter image description here

The sequence starts at the top left and ends in the middle.

There is still some level of uneasiness here which may be alleviated if one uses both the previous and the next point to adjust for both angles, but that would also mean to draw one point less than what one has got. I find this result already satisfactory, so I didn't try.

John
  • 6,693
  • 3
  • 51
  • 90
1

I think you need to look into the Device Context documentation for wxWidgets.

I have some code that draws like this:

//screenArea is a wxStaticBitmap
int startx, starty;
void OnMouseDown(wxMouseEvent& event)
{
    screenArea->CaptureMouse();
    xstart = event.GetX();
    ystart = event.GetY();
    event.Skip();
}
void OnMouseMove(wxMouseEvent& event)
{
    if(event.Dragging() && event.LeftIsDown())
    {
        wxClientDC dc(screenArea);
        dc.SetPen(*wxBLACK_PEN);
        dc.DrawLine(startx, starty, event.GetX(), event.GetY());
    }
    startx = event.GetX();
    starty = event.GetY();
    event.Skip();
}

I know it's C++ but you said the language was irrelevant, so I hope it helps anyway.

This lets me do this:

alt text

which seems significantly smoother than your example.

Community
  • 1
  • 1
Evan Cordell
  • 4,108
  • 2
  • 31
  • 47
  • Thanks for your help, but I would like to draw something that's smoother than lines. Either that or have the mouse move events appear more often. – Heinrich Apfelmus Jul 28 '10 at 16:32
  • 1
    Perhaps I'm misunderstanding; this is pretty smooth. See this image: http://farm5.static.flickr.com/4108/4837817079_a0c191586d.jpg for an example (I used the code above). What do you mean by smoother? Wider and anti-aliased? – Evan Cordell Jul 28 '10 at 17:49
  • Ah, ok, that looks smooth, i.e. the line segments are not visible. It appears you're getting more mouse move events, probably because I'm using a different setup. Edited my question. It appears that `wxStaticBitmap` has a maximum size of 64x64; do you have another recommendation for the screen area? – Heinrich Apfelmus Jul 28 '10 at 20:10
  • 1
    The 64x64 restriction only applies to Windows 9x, which I assumed you weren't targeting. wxStaticBitmap should be fine for pretty much anything. However, if you want something really efficient, the best way to manipulate image data in wxWidgets that I've found is to store an array of bytes (I use unsigned char in c++), where each byte is a the Red, Green, or Blue value for a point. Then you can load that into a wxImage and display in whatever container you want. I know that wasn't an excellent explanation; just let me know if you want to go that route and I'll add an answer explaining it. – Evan Cordell Jul 29 '10 at 19:52
  • I'm currently storing the drawing in a wxBitmap and copying that into a wxScrolledWindow every now and then. From your comment, I gather that this approach is reasonably efficient, thanks! – Heinrich Apfelmus Jul 30 '10 at 13:12
  • On the matter of smoothness, the lines segments turn up when the mouse is moved very rapidly. If I move my mouse fast enough, I get them to show up in the GIMP, too. So, using lines for interpolation is standard, it's just that wxWidgets doesn't fire mouse move events fast enough for me. I estimate that it fires a mouse move event every 50ms. Is there a straightforward way to lower that number to say 20ms? – Heinrich Apfelmus Jul 30 '10 at 13:19
  • 1
    I haven't timed wxWidgets to see what performance I get, but I'd guess that I get closer to 20ms than 50ms. The issue might be some bottleneck that occurs going wxWidgets->Haskell? Unless you're on older hardware, I'd imagine that's the case. – Evan Cordell Jul 30 '10 at 14:47
  • I'm not sure. Evan, could you post the code you already have as an answer to my [new question](http://stackoverflow.com/questions/3484058/example-code-for-a-paint-program-a-la-ms-paint) that asks for example code? – Heinrich Apfelmus Aug 14 '10 at 15:56
1

Interpolating mouse movements with line segments is fine, GIMP does it that way, too, as the following screenshot from a very fast mouse movement shows:

GIMP uses line segments, too

So, smoothness comes from a high frequency of mouse move events. WxWidgets can do that, as the example code for a related question demonstrates.

The problem is in your code, Heinrich. Namely, drawing into a large bitmap first and then copying the whole bitmap to the screen is not cheap! To estimate how efficient you need to be, compare your problem to video games: a smooth rate of 30 mouse move events per second correspond to 30fps. Copying a double buffer is no problem for modern machines, but WxHaskell is likely not optimized for video games, so it's not surprising that you experience some jitter.

The solution is to draw only as much as necessary, i.e. just the lines, directly on the screen, for example as shown in the link above.

Community
  • 1
  • 1
Heinrich Apfelmus
  • 11,034
  • 1
  • 39
  • 67
  • How is the code you posted any different from mine, only without interpolating between dots? – Evan Cordell Aug 30 '10 at 15:24
  • My code in the [related question](http://stackoverflow.com/questions/3484058/example-code-for-a-minimal-paint-program-ms-paint-style/3487944#3487944) is probably almost the same as yours. (I'd accept yours if you post it there). My Haskell code in the question here is different in that it copies a 800x600 bitmap to the screen on every mouse move. – Heinrich Apfelmus Aug 30 '10 at 16:07