From 26a98d027bfb4c65fbc0095b96b99e0732fcc671 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 5 Jul 2022 11:32:45 -0700 Subject: make freehand drawings smoother Now I might actually use them more, and maybe I can start considering taking out some shapes. Do I really need circles if I don't provide ellipses? Thanks Ivan Reese for the feedback. "What drawings does your tool encourage?" Minor note: taking out the deepcopy creates a cute little string like effect, where the curve grows tighter the slower you draw it. --- drawing.lua | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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 -- cgit 1.4.1-2-gfad0