How do you deal with problem polygons?

I have some code which generates a series of polygons and am having trouble getting some of them to render properly. This is because the simulator keeps complaining…

WARNING: Polygon could not be generated. The polygon outline is invalid, possibly due to holes or self-intersection.

What I can’t find it is why it’s saying this. Obviously all the points in the polygon can be dumped to the console, various things can be done to smooth the polygon out, etc, but actually finding out what the sim is referring to is very difficult.

How do you solve these problems? Do you have any tricks that are worth sharing?

I’ll go first. My method is to check for any 0-length path elements:

-- returns shallow copy of table elements from index for count of size (or to end if size is nil) table.range = function( tbl, index, size ) if (index == nil or index \< 1) then return nil end size = size or #tbl-index+1 local output = {} for i=index, index+size-1 do output[#output+1] = tbl[i] end return output end -- trims any points which are a duplicate of their preceding point local function trim( path ) local output = {} for i=1, #path-1, 2 do local pts = table.range( path, i, 4 ) if (#pts == 2) then pts = table.copy( pts, table.range( path, 1, 2 ) ) end if (math.lengthOf( unpack( pts ) ) \> 0) then output[#output+1] = pts[1] output[#output+1] = pts[2] end end return output end

Of course, an extra check would be to test for self-intersections within the polygon, but I find that the above check seems to solve most of my polygon problems, for now.

I’ll go first. My method is to check for any 0-length path elements:

-- returns shallow copy of table elements from index for count of size (or to end if size is nil) table.range = function( tbl, index, size ) if (index == nil or index \< 1) then return nil end size = size or #tbl-index+1 local output = {} for i=index, index+size-1 do output[#output+1] = tbl[i] end return output end -- trims any points which are a duplicate of their preceding point local function trim( path ) local output = {} for i=1, #path-1, 2 do local pts = table.range( path, i, 4 ) if (#pts == 2) then pts = table.copy( pts, table.range( path, 1, 2 ) ) end if (math.lengthOf( unpack( pts ) ) \> 0) then output[#output+1] = pts[1] output[#output+1] = pts[2] end end return output end

Of course, an extra check would be to test for self-intersections within the polygon, but I find that the above check seems to solve most of my polygon problems, for now.