Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Passing stuff to functions (for dummies) — Gideros Forum

Passing stuff to functions (for dummies)

antixantix Member
edited September 2015 in General questions
Hi guys. I wonder if anyone can tell me how I can pass the newPolygon() function in the following code a table of values as opposed to inline values.

local verts = {0,0, 100,0, 100,100, 0,100}
local thisworks = newPoly(0,0, 100,0, 100,100, 0,100)
local thisfails = newPoly(verts)

I'm too stupid or too tired to fgure it out, can anyone assist?
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

Sign In or Register to comment.