在MS Paint上编写绘画程序-如何在鼠标移动事件之间进行插补? [英] Writing a paint program à la MS Paint - how to interpolate between mouse move events?

查看:106
本文介绍了在MS Paint上编写绘画程序-如何在鼠标移动事件之间进行插补?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以MS Paint的风格编写绘画程序.

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

现在我的问题:按照我的喜好,用直线段进行插值看起来参差不齐,您能推荐一种更好的插值方法吗? GIMP或Adobe Photoshop采用什么方法?

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?

或者,是否可以增加我收到的鼠标移动事件的频率?我正在使用的GUI框架是 wxWidgets .

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框架:wxWidgets.
(编程语言:Haskell,但这无关紧要)

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

澄清:我想要看起来比直线段更平滑的东西,请看图片(原始尺寸):

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

我正在使用的代码如下:

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 []

这可能会有所作为.也许我对wx类的选择是为什么出现鼠标移动事件的频率较低的原因.

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

推荐答案

用线段内插鼠标移动很好,GIMP也是这样做的,如以下快速移动鼠标的屏幕截图所示:

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:

因此,平滑度来自高频率的鼠标移动事件. WxWidgets可以做到这一点,作为

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

问题出在您的代码中,海因里希(Heinrich).也就是说,先绘制一个大的位图,然后将整个位图复制到屏幕上并不便宜!要估算您需要提高的效率,请将您的问题与视频游戏进行比较:每秒30次鼠标移动事件的平稳速率相当于30fps.复制双缓冲区对于现代计算机而言没有问题,但是WxHaskell可能并未针对视频游戏进行优化,因此遇到抖动也就不足为奇了.

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.

这篇关于在MS Paint上编写绘画程序-如何在鼠标移动事件之间进行插补?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆