Fastest way to fill pixels between two points with an ellipse-shaped brush?
Say I'm creating a raster-based paint program. My brush is ellipse-shaped. The user clicks and then drags the mouse to paint an area. I now have two points: pointA (where they clicked) and pointB (the first point returned from the mouse-drag). I want to fill in all pixels that fall within the brush region. What's the best way to do this?
My first approach is to calculate the slope between the two points and then increment from pointA to pointB. For each point in between, I search for all pixels within range of the ellipse and turn them on if they are in range. But this seems inefficient, since pixels will often be checked more than once -- often several times over, since the increment is much smaller than the ellipse (think Venn diagram).
Is there a better approach to this? One that is more efficient and minimizes 开发者_开发技巧the number of times a pixel needs to be checked.
I am currently doing something very similar and can tell you what i am doing:
- Mouse moves and I receive points(x,y) where the mouse is
- I use the Bresenham algorithm to estimate lines from one mouse position to the next (I don't get mouse move event for every pixel).
- Then I "stamp" (aka draw) my brush tip (circle) along the line with a proper spacing (20% of Brush).
Because I don't know what programming language/framework you use, I can't give you code :)
精彩评论