I wonder whats the best solution so simplify a set of verts by an angle. Say, I have a table with 90 verts and want only each vert, which is in range of a certain angle - say 8 degree! How would I approach?
For now, I’ve tried the below solution, which doesn’t work properly. Angles are positive/negative/positive/… and my function does only 3/4 of the job. Last 1/4 vertices are miraculously missing. It just stops there. Why? Or can you provide a better algorithm?
Thank you in advance
-- Scans through non-transparent image pixels and creates a simplified bounding polygon for later use with physics!
local function vectorise(img)
local verts = {}
local function scan(opposite)
local row = {
of = 1,
to = img.height,
step = 1
}
local col = {
of = 1,
to = img.width,
step = 1
}
if opposite then
row.of, row.to, row.step = img.height, 1, -1
col.of, col.to, col.step = img.width, 1, -1
end
-- scan through both halves of the image (right + left) and collect bounding vertices
for v = row.of, row.to, row.step do --rows
for h = col.of, col.to, col.step do --columns
local curr = vec2(h, v)
local r, g, b, a = img:get(curr.x, curr.y)
if a > 0 then --ignore only transparent pixels!
table.insert(verts, curr - vec2(1,1))
break
end
end
end
end
scan(true) --works anticlockwise
scan()
-- simplify the bounding shape
local skipangle = 8
local comparative = verts[1]
local v = {comparative}
for i = 2, #verts do
local point = verts[i]
local angle = math.deg(comparative:angleBetween(point))
if angle > skipangle then
table.insert(v, point)
comparative = point
end
end
--return verts
return v
end