about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-11 22:27:47 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-11 22:27:47 -0700
commit61292c439bf5549d195abaa9c32dc627a108bb6c (patch)
tree7c8bcfd538561eb5c6ab7d24f743f9e624ce1bc4
parent91fe5cd98061af426505319e826d76d5b8ad4daa (diff)
downloadlines.love-61292c439bf5549d195abaa9c32dc627a108bb6c.tar.gz
simplest possible way to straighten strokes
-rw-r--r--main.lua21
1 files changed, 21 insertions, 0 deletions
diff --git a/main.lua b/main.lua
index accc980..afd4b32 100644
--- a/main.lua
+++ b/main.lua
@@ -171,8 +171,29 @@ function keychord_pressed(chord)
     lines[#lines+1] = ''
   elseif chord == 'C-d' then
     parse_into_exec_payload(lines[#lines])
+  elseif chord == 'C-l' then
+    for i,drawing in ipairs(lines) do
+      if type(drawing) == 'table' then
+        local x, y = love.mouse.getX(), love.mouse.getY()
+        if y >= drawing.y and y < drawing.y + drawing.h and x >= 12 and x < 12+drawing.w then
+          for j,shape in ipairs(drawing.shapes) do
+            if on_freehand(love.mouse.getX(),love.mouse.getY(), shape) then
+              convert_line(drawing,j,shape)
+            end
+          end
+        end
+      end
+    end
   end
 end
 
+function convert_line(drawing, i, shape)
+  -- Perhaps we should do a more sophisticated "simple linear regression"
+  -- here:
+  --   https://en.wikipedia.org/wiki/Linear_regression#Simple_and_multiple_linear_regression
+  -- But this works well enough for close-to-linear strokes.
+  drawing.shapes[i] = {shape[1], shape[#shape]}
+end
+
 function love.keyreleased(key, scancode)
 end