-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstipple.lua
127 lines (66 loc) · 2.27 KB
/
stipple.lua
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
local stipple = {}
local args = {...}
local lg = love.graphics
local dist = function(x1, y1, x2, y2) return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) end
local imgD = love.image.newImageData(1,8)
imgD:mapPixel(function() return 255,255,255,255 end)
stipple.img = lg.newImage(imgD)
stipple.img:setWrap('repeat', 'repeat')
stipple.quad = lg.newQuad(0,0,1,8,1,8)
imgD = nil
local line_w,line_s
stipple.setLine = function(width,style)
line_w,line_s=width/eye.s,style
lg._setLine(line_w,line_s)
end
stipple.draw = function(self, x1, y1, x2, y2)
if type(x1) == 'table' then
local t = x1
for i=1, #t-2, 2 do
self:line(t[i], t[i+1], t[i+2], t[i+3])
end
else
self:line(x1,y1,x2,y2)
end
end
stipple.line = function(self, x1,y1,x2,y2)
local d = dist(x1,y1,x2,y2)
local v = {self.quad:getViewport()}
local lw, ls = line_w, line_s == 'rough' and 'nearest' or 'linear'
self.img:setFilter(ls, ls)
self.quad:setViewport(v[1], v[2], v[3], d)
local a = math.atan2(y2-y1, x2-x1)-math.pi/2
lg.drawq(self.img, self.quad, x1, y1, a, lw, 1)
end
stipple.stipples = {}
stipple.setStipple = function(self, stipple)
assert(type(stipple) == 'number' or (type(stipple) == 'string' and tonumber(stipple)), "Wrong stipple type - binary string expected, got "..type(stipple))
stipple = tostring(stipple)
if stipple:find "[23456789%.]" then
error "Invalid stipple - integral binary string expected, found other figures"
end
local h = #stipple
if not self.stipples[stipple] then
local imgD = love.image.newImageData(1, h)
for i = 1, h do
local v = stipple:sub(i,i)
imgD:setPixel(0, i-1, v*255, v*255, v*255, v*255)
end
self.stipples[stipple] = {img = lg.newImage(imgD), quad = lg.newQuad(0,0,1,h,1,h)}
self.stipples[stipple].img:setWrap('repeat', 'repeat')
end
self.img = self.stipples[stipple].img
self.quad = self.stipples[stipple].quad
self.stipple = stipple
end
stipple.next = function(self)
self:setStipple(self.stipple:sub(-1,-1)..self.stipple:sub(1, -2))
end
stipple.prev = function(self)
self:setStipple(self.stipple:sub(2, -1)..self.stipple:sub(1,1))
end
stipple.previous = stipple.prev
if tonumber(args[1]) and not args[1]:find "[23456789%.]" then
stipple:setStipple(tostring(args[1]))
end
return stipple