diff options
-rw-r--r-- | drawing.lua | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/drawing.lua b/drawing.lua index cff5620..3ed1b3f 100644 --- a/drawing.lua +++ b/drawing.lua @@ -116,7 +116,9 @@ function Drawing.draw_pending_shape(left,top, drawing) if shape.mode == nil then -- nothing pending elseif shape.mode == 'freehand' then - Drawing.draw_shape(left,top, drawing, shape) + local shape_copy = deepcopy(shape) + Drawing.smoothen(shape_copy) + Drawing.draw_shape(left,top, drawing, shape_copy) elseif shape.mode == 'line' then local mx,my = Drawing.coord(App.mouse_x()-left), Drawing.coord(App.mouse_y()-top) if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then @@ -291,6 +293,7 @@ function Drawing.mouse_released(x,y, button) -- nothing pending elseif drawing.pending.mode == 'freehand' then -- the last point added during update is good enough + Drawing.smoothen(drawing.pending) table.insert(drawing.shapes, drawing.pending) elseif drawing.pending.mode == 'line' then local mx,my = Drawing.coord(x-Margin_left), Drawing.coord(y-drawing.y) @@ -673,6 +676,19 @@ function Drawing.contains_point(shape, p) end end +function Drawing.smoothen(shape) + assert(shape.mode == 'freehand') + for _=1,7 do + for i=2,#shape.points-1 do + local a = shape.points[i-1] + local b = shape.points[i] + local c = shape.points[i+1] + b.x = (a.x + b.x + c.x)/3 + b.y = (a.y + b.y + c.y)/3 + end + end +end + function Drawing.insert_point(points, x,y) for i,point in ipairs(points) do if Drawing.near(point, x,y) then |