about summary refs log tree commit diff stats
path: root/drawing.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-17 21:18:17 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-17 21:34:55 -0700
commitde495ae0f1574ee1f0fb803ff05c77aaf04f13fc (patch)
tree9fc98b05410ac66763d390703b5ff8b78eba6f68 /drawing.lua
parent91ce333ae0f617ef4eb6638faf66bbe159f25a0d (diff)
downloadlines.love-de495ae0f1574ee1f0fb803ff05c77aaf04f13fc.tar.gz
several more modules
This is probably not ideal; let's see how it goes..
Diffstat (limited to 'drawing.lua')
-rw-r--r--drawing.lua256
1 files changed, 244 insertions, 12 deletions
diff --git a/drawing.lua b/drawing.lua
index 928325f..30182a2 100644
--- a/drawing.lua
+++ b/drawing.lua
@@ -1,15 +1,16 @@
 -- primitives for editing drawings
 Drawing = {}
+geom = require 'geom'
 
 function Drawing.draw(line, y)
   local pmx,pmy = love.mouse.getX(), love.mouse.getY()
-  if pmx < 16+Drawing_width and pmy > line.y and pmy < line.y+pixels(line.h) then
+  if pmx < 16+Drawing_width and pmy > line.y and pmy < line.y+Drawing.pixels(line.h) then
     love.graphics.setColor(0.75,0.75,0.75)
-    love.graphics.rectangle('line', 16,line.y, Drawing_width,pixels(line.h))
-    if icon[Current_mode] then
-      icon[Current_mode](16+Drawing_width-20, line.y+4)
+    love.graphics.rectangle('line', 16,line.y, Drawing_width,Drawing.pixels(line.h))
+    if icon[Current_drawing_mode] then
+      icon[Current_drawing_mode](16+Drawing_width-20, line.y+4)
     else
-      icon[Previous_mode](16+Drawing_width-20, line.y+4)
+      icon[Previous_drawing_mode](16+Drawing_width-20, line.y+4)
     end
 
     if love.mouse.isDown('1') and love.keyboard.isDown('h') then
@@ -23,29 +24,260 @@ function Drawing.draw(line, y)
     return
   end
 
-  local mx,my = coord(love.mouse.getX()-16), coord(love.mouse.getY()-line.y)
+  local mx,my = Drawing.coord(love.mouse.getX()-16), Drawing.coord(love.mouse.getY()-line.y)
 
   for _,shape in ipairs(line.shapes) do
     assert(shape)
-    if on_shape(mx,my, line, shape) then
+    if geom.on_shape(mx,my, line, shape) then
       love.graphics.setColor(1,0,0)
     else
       love.graphics.setColor(0,0,0)
     end
-    draw_shape(16,line.y, line, shape)
+    Drawing.draw_shape(16,line.y, line, shape)
   end
   for _,p in ipairs(line.points) do
     if p.deleted == nil then
-      if near(p, mx,my) then
+      if Drawing.near(p, mx,my) then
         love.graphics.setColor(1,0,0)
-        love.graphics.circle('line', pixels(p.x)+16,pixels(p.y)+line.y, 4)
+        love.graphics.circle('line', Drawing.pixels(p.x)+16,Drawing.pixels(p.y)+line.y, 4)
       else
         love.graphics.setColor(0,0,0)
-        love.graphics.circle('fill', pixels(p.x)+16,pixels(p.y)+line.y, 2)
+        love.graphics.circle('fill', Drawing.pixels(p.x)+16,Drawing.pixels(p.y)+line.y, 2)
       end
     end
   end
-  draw_pending_shape(16,line.y, line)
+  Drawing.draw_pending_shape(16,line.y, line)
+end
+
+function Drawing.current_drawing()
+  local x, y = love.mouse.getX(), love.mouse.getY()
+  for _,drawing in ipairs(Lines) do
+    if drawing.mode == 'drawing' then
+      if y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= 16 and x < 16+Drawing_width then
+        return drawing
+      end
+    end
+  end
+  return nil
+end
+
+function Drawing.select_shape_at_mouse()
+  for _,drawing in ipairs(Lines) do
+    if drawing.mode == 'drawing' then
+      local x, y = love.mouse.getX(), love.mouse.getY()
+      if y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= 16 and x < 16+Drawing_width then
+        local mx,my = Drawing.coord(love.mouse.getX()-16), Drawing.coord(love.mouse.getY()-drawing.y)
+        for i,shape in ipairs(drawing.shapes) do
+          assert(shape)
+          if geom.on_shape(mx,my, drawing, shape) then
+            return drawing,i,shape
+          end
+        end
+      end
+    end
+  end
+end
+
+function Drawing.select_point_at_mouse()
+  for _,drawing in ipairs(Lines) do
+    if drawing.mode == 'drawing' then
+      local x, y = love.mouse.getX(), love.mouse.getY()
+      if y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= 16 and x < 16+Drawing_width then
+        local mx,my = Drawing.coord(love.mouse.getX()-16), Drawing.coord(love.mouse.getY()-drawing.y)
+        for i,point in ipairs(drawing.points) do
+          assert(point)
+          if Drawing.near(point, mx,my) then
+            return drawing,i,point
+          end
+        end
+      end
+    end
+  end
+end
+
+function Drawing.select_drawing_at_mouse()
+  for _,drawing in ipairs(Lines) do
+    if drawing.mode == 'drawing' then
+      local x, y = love.mouse.getX(), love.mouse.getY()
+      if y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= 16 and x < 16+Drawing_width then
+        return drawing
+      end
+    end
+  end
+end
+
+function Drawing.contains_point(shape, p)
+  if shape.mode == 'freehand' then
+    -- not supported
+  elseif shape.mode == 'line' or shape.mode == 'manhattan' then
+    return shape.p1 == p or shape.p2 == p
+  elseif shape.mode == 'polygon' then
+    return table.find(shape.vertices, p)
+  elseif shape.mode == 'circle' then
+    return shape.center == p
+  elseif shape.mode == 'arc' then
+    return shape.center == p
+    -- ugh, how to support angles
+  elseif shape.mode == 'deleted' then
+    -- already done
+  else
+    print(shape.mode)
+    assert(false)
+  end
+end
+
+function Drawing.convert_line(drawing, 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.
+  assert(shape.mode == 'freehand')
+  shape.mode = 'line'
+  shape.p1 = insert_point(drawing.points, shape.points[1].x, shape.points[1].y)
+  local n = #shape.points
+  shape.p2 = insert_point(drawing.points, shape.points[n].x, shape.points[n].y)
+end
+
+-- turn a line either horizontal or vertical
+function Drawing.convert_horvert(drawing, shape)
+  if shape.mode == 'freehand' then
+    convert_line(shape)
+  end
+  assert(shape.mode == 'line')
+  local p1 = drawing.points[shape.p1]
+  local p2 = drawing.points[shape.p2]
+  if math.abs(p1.x-p2.x) > math.abs(p1.y-p2.y) then
+    p2.y = p1.y
+  else
+    p2.x = p1.x
+  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
+      return i
+    end
+  end
+  table.insert(points, {x=x, y=y})
+  return #points
+end
+
+function Drawing.near(point, x,y)
+  local px,py = Drawing.pixels(x),Drawing.pixels(y)
+  local cx,cy = Drawing.pixels(point.x), Drawing.pixels(point.y)
+  return (cx-px)*(cx-px) + (cy-py)*(cy-py) < 16
+end
+
+function Drawing.draw_shape(left,top, drawing, shape)
+  if shape.mode == 'freehand' then
+    local prev = nil
+    for _,point in ipairs(shape.points) do
+      if prev then
+        love.graphics.line(Drawing.pixels(prev.x)+left,Drawing.pixels(prev.y)+top, Drawing.pixels(point.x)+left,Drawing.pixels(point.y)+top)
+      end
+      prev = point
+    end
+  elseif shape.mode == 'line' or shape.mode == 'manhattan' then
+    local p1 = drawing.points[shape.p1]
+    local p2 = drawing.points[shape.p2]
+    love.graphics.line(Drawing.pixels(p1.x)+left,Drawing.pixels(p1.y)+top, Drawing.pixels(p2.x)+left,Drawing.pixels(p2.y)+top)
+  elseif shape.mode == 'polygon' then
+    local prev = nil
+    for _,point in ipairs(shape.vertices) do
+      local curr = drawing.points[point]
+      if prev then
+        love.graphics.line(Drawing.pixels(prev.x)+left,Drawing.pixels(prev.y)+top, Drawing.pixels(curr.x)+left,Drawing.pixels(curr.y)+top)
+      end
+      prev = curr
+    end
+    -- close the loop
+    local curr = drawing.points[shape.vertices[1]]
+    love.graphics.line(Drawing.pixels(prev.x)+left,Drawing.pixels(prev.y)+top, Drawing.pixels(curr.x)+left,Drawing.pixels(curr.y)+top)
+  elseif shape.mode == 'circle' then
+    local center = drawing.points[shape.center]
+    love.graphics.circle('line', Drawing.pixels(center.x)+left,Drawing.pixels(center.y)+top, Drawing.pixels(shape.radius))
+  elseif shape.mode == 'arc' then
+    local center = drawing.points[shape.center]
+    love.graphics.arc('line', 'open', Drawing.pixels(center.x)+left,Drawing.pixels(center.y)+top, Drawing.pixels(shape.radius), shape.start_angle, shape.end_angle, 360)
+  elseif shape.mode == 'deleted' then
+  else
+    print(shape.mode)
+    assert(false)
+  end
+end
+
+function Drawing.draw_pending_shape(left,top, drawing)
+  local shape = drawing.pending
+  if shape.mode == 'freehand' then
+    draw_shape(left,top, drawing, shape)
+  elseif shape.mode == 'line' then
+    local p1 = drawing.points[shape.p1]
+    local mx,my = Drawing.coord(love.mouse.getX()-16), Drawing.coord(love.mouse.getY()-drawing.y)
+    if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then
+      return
+    end
+    love.graphics.line(Drawing.pixels(p1.x)+left,Drawing.pixels(p1.y)+top, Drawing.pixels(mx)+left,Drawing.pixels(my)+top)
+  elseif shape.mode == 'manhattan' then
+    local p1 = drawing.points[shape.p1]
+    local mx,my = Drawing.coord(love.mouse.getX()-16), Drawing.coord(love.mouse.getY()-drawing.y)
+    if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then
+      return
+    end
+    if math.abs(mx-p1.x) > math.abs(my-p1.y) then
+      love.graphics.line(Drawing.pixels(p1.x)+left,Drawing.pixels(p1.y)+top, Drawing.pixels(mx)+left,Drawing.pixels(p1.y)+top)
+    else
+      love.graphics.line(Drawing.pixels(p1.x)+left,Drawing.pixels(p1.y)+top, Drawing.pixels(p1.x)+left,Drawing.pixels(my)+top)
+    end
+  elseif shape.mode == 'polygon' then
+    -- don't close the loop on a pending polygon
+    local prev = nil
+    for _,point in ipairs(shape.vertices) do
+      local curr = drawing.points[point]
+      if prev then
+        love.graphics.line(Drawing.pixels(prev.x)+left,Drawing.pixels(prev.y)+top, Drawing.pixels(curr.x)+left,Drawing.pixels(curr.y)+top)
+      end
+      prev = curr
+    end
+    love.graphics.line(Drawing.pixels(prev.x)+left,Drawing.pixels(prev.y)+top, love.mouse.getX(),love.mouse.getY())
+  elseif shape.mode == 'circle' then
+    local center = drawing.points[shape.center]
+    local mx,my = Drawing.coord(love.mouse.getX()-16), Drawing.coord(love.mouse.getY()-drawing.y)
+    if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then
+      return
+    end
+    local cx,cy = Drawing.pixels(center.x)+left, Drawing.pixels(center.y)+top
+    love.graphics.circle('line', cx,cy, math.dist(cx,cy, love.mouse.getX(),love.mouse.getY()))
+  elseif shape.mode == 'arc' then
+    local center = drawing.points[shape.center]
+    local mx,my = Drawing.coord(love.mouse.getX()-16), Drawing.coord(love.mouse.getY()-drawing.y)
+    if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then
+      return
+    end
+    shape.end_angle = geom.angle_with_hint(center.x,center.y, mx,my, shape.end_angle)
+    local cx,cy = Drawing.pixels(center.x)+left, Drawing.pixels(center.y)+top
+    love.graphics.arc('line', 'open', cx,cy, Drawing.pixels(shape.radius), shape.start_angle, shape.end_angle, 360)
+  end
+end
+
+function Drawing.pixels(n)  -- parts to pixels
+  return n*Drawing_width/256
+end
+function Drawing.coord(n)  -- pixels to parts
+  return math.floor(n*256/Drawing_width)
 end
 
 return Drawing
;id=3a49e7e992b23d212b0c1df9e93c6564d4823ef2'>^
e3cb4d09 ^

5ba1c3b9 ^
e3cb4d09 ^


5ba1c3b9 ^
795f5244 ^
5ba1c3b9 ^
e3cb4d09 ^
a4f12265 ^
17b90929 ^
a4f12265 ^





a09697d0 ^
a4f12265 ^



3adc9e08 ^
b954ac38 ^




a4f12265 ^


17b90929 ^
a09697d0 ^










17b90929 ^
7637d1ac ^




5ba1c3b9 ^
7637d1ac ^



a09697d0 ^

b954ac38 ^

5ba1c3b9 ^
a09697d0 ^












5ba1c3b9 ^
5d4a1d3b ^
a09697d0 ^

5d4a1d3b ^
b954ac38 ^
5d4a1d3b ^

5ba1c3b9 ^

a9a23371 ^
b86707a9 ^
a9a23371 ^
b86707a9 ^
a9a23371 ^
5ba1c3b9 ^





a09697d0 ^
5ba1c3b9 ^
a09697d0 ^

a9a23371 ^
a09697d0 ^
a9a23371 ^
a09697d0 ^


5ba1c3b9 ^





a9a23371 ^
5d4a1d3b ^





17b90929 ^
5d4a1d3b ^












17b90929 ^
a4f12265 ^










3adc9e08 ^
b954ac38 ^




a4f12265 ^


17b90929 ^
a4f12265 ^












5ba1c3b9 ^

























































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382