function on_freehand(x,y, shape)
local prev
for _,p in ipairs(shape) do
if prev then
if on_line(x,y, {x1=prev.x,y1=prev.y, x2=p.x,y2=p.y}) then
return true
end
end
prev = p
end
return false
end
function on_line(x,y, shape)
if shape.x1 == shape.x2 then
local y1,y2 = shape.y1,shape.y2
if y1 > y2 then
y1,y2 = y2,y2
end
return y >= y1 and y <= y2
end
-- has the right slope and intercept
local m = (shape.y2 - shape.y1) / (shape.x2 - shape.x1)
local yp = shape.y1 + m*(x-shape.x1)
if yp < 0.95*y or yp > 1.05*y then
return false
end
-- between endpoints
local k = (x-shape.x1) / (shape.x2-shape.x1)
return k > -0.05 and k < 1.05
end