Repository navigation
Expand file tree
/
Copy pathDrawCommon.lua
More file actions
385 lines (326 loc) · 6.33 KB
/
Copy pathDrawCommon.lua
File metadata and controls
385 lines (326 loc) · 6.33 KB
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
383
384
385
function CreateItemBase(t,c)
return {nPix = 0,type = t,c = c,pts = {}}
end
function CreateItem(cmd)
if gItemFactory == nil then
gItemFactory = {
["l"] = CreatePolyLine,
["s"] = CreateSpline,
["f"] = CreateFill,
-- ["e"] = true,
-- ["c"] = true,
}
end
local item
local fnCreate = gItemFactory[cmd]
if fnCreate then
item = CreateItemBase(cmd)
fnCreate(item)
end
return item
end
function PlotLine(x0,y0,x1,y1,c,fn)
if fn == nil then fn = pix end
local dx = abs(x1-x0)
local dy = -abs(y1-y0)
local sx,sy
if x0 < x1 then sx = 1 else sx = -1 end
if y0 < y1 then sy = 1 else sy = -1 end
local err = dx+dy -- error value e_xy
local e2 = err
local b = true
local iPix = 0
while (b) do
iPix = iPix+1
fn(x0,y0,c)
e2 = 2*err
if e2 >= dy then -- e_xy+e_x > 0
if x0 == x1 then b = false end
err = err+dy
x0 = x0+sx
end
if e2 <= dx then -- e_xy+e_y < 0
if y0 == y1 then b = false end
err = err+dx
y0 = y0+sy
end
end
return iPix
end
function CreatePolyLine(item)
item.i = 1
function item.Load(_,p)
_.c = p[1]
local ptcount = (#p-1)>>1
for i = 1,ptcount do
_.pts[i] = {p[i*2],p[1+i*2]}
end
if #_.pts == 1 then
_.pts[2] = _.pts[1]
end
end
function item.Save(_)
local s = {}
table.insert(s,_.c)
for k,v in pairs(_.pts) do
table.insert(s,v[1])
table.insert(s,v[2])
end
return s
end
function item.InitSeg(_,i)
_.i = i
if #_.pts < 2 then return end
local p0 = _.pts[i]
local p1 = _.pts[i+1]
_.x = p0[1]
_.y = p0[2]
_.x1 = p1[1]
_.y1 = p1[2]
_.dx = abs(_.x1-_.x)
_.dy = -abs(_.y1-_.y)
if _.x < _.x1 then _.sx = 1 else _.sx = -1 end
if _.y < _.y1 then _.sy = 1 else _.sy = -1 end
_.err = _.dx+_.dy -- error value e_xy
_.e2 = _.err
end
function item.Init(_)
_:InitSeg(1)
end
function item.Draw(_,fnPix)
if fnPix ~= nil then
fnPix(_.x,_.y,_.c)
end
while _.x == _.x1 and _.y == _.y1 do -- completed line ?
local i = _.i+1
if i >= #_.pts then
return 0 -- was last segment
else
_:InitSeg(i) -- next segment
end
end
_.e2 = 2*_.err
if _.e2 >= _.dy then -- e_xy+e_x > 0
_.err = _.err+_.dy
_.x = _.x+_.sx
end
if _.e2 <= _.dx then -- e_xy+e_y < 0
_.err = _.err+_.dx
_.y = _.y+_.sy
end
return 1
end
return item
end
function CreateSpline(item)
item.t = 0
item.i = 0
item.tend = 0
item.keys = {}
item.div = 3
function item.Load(_,p)
_.c = p[1]
local ptcount = (#p-1)>>1
for i = 1,ptcount do
_.pts[i] = {p[i*2],p[1+i*2]}
end
end
function item.Save(_)
local s = {}
table.insert(s,_.c)
for k,v in pairs(_.pts) do
table.insert(s,v[1])
table.insert(s,v[2])
end
return s
end
function item.Init(_)
_.i = 0
_.t = 0
_.keys = {} -- build up CatmullRom keys
local t = 0
local x1,y1,x2,y2
local div = _.div
for k,v in pairs(_.pts) do
x2 = v[1]
y2 = v[2]
if x1 ~= nil then
local d = distance(x1,y1,x2,y2)
d = floor(d/div+1)*div
t = t+d
end
table.insert(_.keys,t)
table.insert(_.keys,x2)
table.insert(_.keys,y2)
x1 = x2
y1 = y2
end
_.tend = t
end
function item.Draw(_,fnPix)
local tprev = _.t
_.t = _.t+_.div
if _.t > _.tend then
_.t = _.tend
end
local v0 = CatmullRom(_.keys,2,tprev)
local v1 = CatmullRom(_.keys,2,_.t)
local iPix = 0
if fnPix ~= nil then
PlotLine(round(v0[1]),round(v0[2]),round(v1[1]),round(v1[2]),_.c,fnPix)
-- PlotLine(round(v0[1]),round(v0[2]),round(v1[1]),round(v1[2]),_.c+2*(_.i&1),fnPix)
end
_.i = _.i+1
if _.t < _.tend then
return 1
else
return 0
end
end
return item
end
function CreateQueue()
local queue = {_queue = {},_pointer = 0,_has = {}}
queue.__index = queue
function queue:new()
return setmetatable({},queue)
end
function queue.push(_,item)
if _._has[item] then return end
_._pointer = _._pointer+1
_._queue[_._pointer] = item
_._has[item] = true
return _
end
function queue.pop(_)
local item = _._queue[1]
_._pointer = _._pointer-1
table.remove(_._queue,1)
_._has[item] = nil
return item
end
function queue.isEmpty(_)
return (_._pointer == 0)
end
function queue.clear(_)
_._queue = {}
_._has = {}
_._pointer = 0
return _
end
return queue
end
function InScreen(x,y)
return x >= 0 and x < gSizeX and y >= 0 and y < gSizeY
end
function Inside(x,y,o)
return pix(x,y) == o and InScreen(x,y)
end
function HorizontalScan(q,lx,rx,y,o)
local span_added = false
local x
for x = lx,rx do
if Inside(x,y,o) == false then
span_added = false
elseif not span_added then
q:push({x,y})
span_added = true
end
end
end
function ComputeProjRatio(a,b,p)
local vAP,vAB = V2Sub(p,a),V2Sub(b,a)
-- local dist=0
local ratio = 0
local sqrLen = V2SqLength(vAB)
if sqrLen == 0 then
-- dist = V2SqLength(vAP);
else
local rDot = V2Dot(vAP,vAB);
ratio = rDot/sqrLen;
-- rProjRatio = clamp(rProjRatio, 0, 1)
-- dist = rProjRatio * sqrt(sqrLength);
end
return ratio;
end
function CreateFill(item)
item.maxpt = 2
function item.Load(_,p)
_.c = p[1]
_.c2 = p[2]
local ptcount = (#p-2)>>1
for i = 1,ptcount do
_.pts[i] = {p[1+i*2],p[2+i*2]}
end
end
function item.Save(_)
local s = {}
table.insert(s,_.c)
table.insert(s,_.c2)
for k,v in pairs(_.pts) do
table.insert(s,v[1])
table.insert(s,v[2])
end
return s
end
function item.Init(_)
local x = _.pts[1][1]
local y = _.pts[1][2]
if not InScreen(x,y) then return end
_.x = x
_.y = y
_.o = pix(x,y)
_.q = CreateQueue()
_.q:push({x,y})
end
function item.GetColor(_,x,y)
if _.c == _.c2 then
return _.c
else
if #_.pts <= 1 then
return _.c
else
local a,b = {_.pts[1][1],_.pts[1][2]},{_.pts[2][1],_.pts[2][2]}
local k = ComputeProjRatio(a,b,{x,y})
if Bayer8x8:IsAbove(k,x,y) then
return _.c2
else
return _.c
end
end
end
end
function item.Draw(_,fnPix)
local q = _.q
if q:isEmpty() then
return 0
end
local o = _.o
local v = q:pop()
local x = v[1]
local y = v[2]
local lx = x
while Inside(lx-1,y,o) do
if fnPix ~= nil then
local c = _:GetColor(lx-1,y)
fnPix(lx-1,y,c)
end
lx = lx-1
end
while Inside(x,y,o) do
if fnPix ~= nil then
local c = _:GetColor(x,y)
fnPix(x,y,c)
end
x = x+1
end
HorizontalScan(q,lx,x-1,y+1,o)
HorizontalScan(q,lx,x-1,y-1,o)
if q:isEmpty() then
return 0
else
return 1
end
end
return item
end