It looks like you're new here. If you want to get involved, click one of these buttons!
antix
Member
local function toVertexList(vertices, x,y, ...) if not (x and y) then return vertices end -- no more arguments vertices[#vertices + 1] = {x = x, y = y} -- set vertex return toVertexList(vertices, ...) -- recurse end local function areCollinear(p, q, r, eps) -- returns true if three vertices lie on a line return math.abs(vector.det(q.x-p.x, q.y-p.y, r.x-p.x,r.y-p.y)) <= (eps or 1e-32) end local function removeCollinear(vertices) -- remove vertices that lie on a line local ret = {} local i,k = #vertices - 1, #vertices for l=1,#vertices do if not areCollinear(vertices[i], vertices[k], vertices[l]) then ret[#ret+1] = vertices[k] end i,k = k,l end return ret end function newPolygon(...) local vertices = removeCollinear( toVertexList({}, ...) ) assert(#vertices >= 3, "Need at least 3 non collinear points to build polygon (got "..#vertices..")") end |
Comments
Likes: antix
Likes: MoKaLux