diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2022-05-14 11:59:54 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2022-05-14 11:59:54 -0700 |
commit | 5e39aea97d54730e6e74f7181c9d462188dcfff4 (patch) | |
tree | 1029e93d228281f167d99d985d73e1de7f3e451d | |
parent | e3eac12f85fb3df15b639302ec65734193178a5b (diff) | |
download | lines.love-5e39aea97d54730e6e74f7181c9d462188dcfff4.tar.gz |
revert: back to freehand
-rw-r--r-- | main.lua | 65 |
1 files changed, 44 insertions, 21 deletions
diff --git a/main.lua b/main.lua index 4ef4458..b2278d4 100644 --- a/main.lua +++ b/main.lua @@ -57,15 +57,25 @@ function love.draw() local mx,my = coord(love.mouse.getX()-12), coord(love.mouse.getY()-line.y) for _,shape in ipairs(line.shapes) do - if on_line(mx,my, shape) then + if on_freehand(mx,my, shape) then love.graphics.setColor(1,0,0) else love.graphics.setColor(0,0,0) end - love.graphics.line(pixels(shape.x1)+12,pixels(shape.y1)+line.y, pixels(shape.x2)+12,pixels(shape.y2)+line.y) + prev = nil + for _,point in ipairs(shape) do + if prev then + love.graphics.line(pixels(prev.x)+12,pixels(prev.y)+line.y, pixels(point.x)+12,pixels(point.y)+line.y) + end + prev = point + end end - if line.pending.x1 then - love.graphics.line(pixels(line.pending.x1)+12,pixels(line.pending.y1)+line.y, love.mouse.getX(),love.mouse.getY()) + prev = nil + for _,point in ipairs(line.pending) do + if prev then + love.graphics.line(pixels(prev.x)+12,pixels(prev.y)+line.y, pixels(point.x)+12,pixels(point.y)+line.y) + end + prev = point end else love.graphics.draw(text, 25,y, 0, 1.5) @@ -87,6 +97,7 @@ function love.update(dt) if type(drawing) == 'table' then local x, y = love.mouse.getX(), love.mouse.getY() if y >= drawing.y and y < drawing.y + pixels(drawing.h) and x >= 12 and x < 12+drawingw then + table.insert(drawing.pending, {x=coord(love.mouse.getX()-12), y=coord(love.mouse.getY()-drawing.y)}) end end end @@ -98,24 +109,11 @@ function love.mousepressed(x,y, button) propagate_to_drawings(x,y, button) end -function love.mousereleased(x,y, button) - if lines.current then - if lines.current.pending then - lines.current.pending.x2 = coord(x-12) - lines.current.pending.y2 = coord(y-lines.current.y) - table.insert(lines.current.shapes, lines.current.pending) - lines.current.pending = {} - lines.current = nil - end - end -end - function propagate_to_drawings(x,y, button) 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 + pixels(drawing.h) and x >= 12 and x < 12+drawingw then - drawing.pending = {x1=coord(x-12), y1=coord(y-drawing.y)} lines.current = drawing end end @@ -157,6 +155,16 @@ function on_line(x,y, shape) return k > -0.05 and k < 1.05 end +function love.mousereleased(x,y, button) + if lines.current then + if lines.current.pending then + table.insert(lines.current.shapes, lines.current.pending) + lines.current.pending = {} + lines.current = nil + end + end +end + function love.textinput(t) lines[#lines] = lines[#lines]..t end @@ -179,6 +187,11 @@ function keychord_pressed(chord) lines[#lines+1] = '' elseif chord == 'C-d' then parse_into_exec_payload(lines[#lines]) + elseif chord == 'C-l' then + local drawing,i,shape = select_shape_at_mouse() + if drawing then + convert_line(drawing,i,shape) + end elseif chord == 'C-m' then local drawing,i,shape = select_shape_at_mouse() if drawing then @@ -194,7 +207,7 @@ function select_shape_at_mouse() if y >= drawing.y and y < drawing.y + pixels(drawing.h) and x >= 12 and x < 12+drawingw then local mx,my = coord(love.mouse.getX()-12), coord(love.mouse.getY()-drawing.y) for i,shape in ipairs(drawing.shapes) do - if on_line(mx,my, shape) then + if on_freehand(mx,my, shape) then return drawing,i,shape end end @@ -203,12 +216,22 @@ function select_shape_at_mouse() 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 + -- turn a stroke into either a horizontal or vertical line function convert_horvert(drawing, i, shape) - if math.abs(shape.x1-shape.x2) > math.abs(shape.y1-shape.y2) then - shape.y2 = shape.y1 + local x1,y1 = shape[1].x, shape[1].y + local x2,y2 = shape[#shape].x, shape[#shape].y + if math.abs(x1-x2) > math.abs(y1-y2) then + drawing.shapes[i] = {{x=x1, y=y1}, {x=x2, y=y1}} else - shape.x2 = shape.x1 + drawing.shapes[i] = {{x=x1, y=y1}, {x=x1, y=y2}} end end |