Polygon could not be generated.

Good morning everybody,
 
I have to display an ellipse in my program. Therefore, I use the display.newPolygon() function to approximate the ellipse with a 512 vertices polygon. Everything is ok, I can display it. The thing is when I need to display a 4 vertices polygon it doesn’t appear and I have the following message :
 

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

 
I have checked that there is no intersection between the lines and no duplicated points. Worse, when I print in the console the coordinates of my vertices table and that I hard-code my vertices table with the printed values I can display the polygon ! I don’t know what is the difference between my vertices table generated with my loop and the vertices table hard-coded.
 
It’s easier to understand with the code :
 

function BuildEllipse(x, y) local i = nil local a = nil local ellipse = { polygon = nil, n = 4, rx = 74.0, ry = 90.0, vertices = {} } -- Here's the loop that creates the ellipse by generating coordinates of each vertex in the ellipse.vertices table -- With that code I CAN'T display the polygon for i = 0, ellipse.n-1, 1 do a = 2\*math.pi\*i/ellipse.n ellipse.vertices[2\*i] = ellipse.rx\*math.cos(a) ellipse.vertices[(2\*i)+1] = -ellipse.ry\*math.sin(a) print("Polygon : X" .. i .. "/Y" .. i .. " : " .. ellipse.vertices[2\*i] .. " ; " .. ellipse.vertices[(2\*i)+1]) end -- Here are the vertices table hard-coded with the printed values of the table generated above -- With that code I CAN display the polygon ellipse.vertices = {74, -0 , 4.5311931568452e-015, -90 , -74 , -1.1021821192326e-014, -1.3593579470536e-014 , 90} ellipse.polygon = display.newPolygon( x, y, ellipse.vertices ) end

May someone have an idea that could answer the question ?

PS : I hope I have been clear enough. Feel free to ask any question if not.

Regards,

Hi.

You have an index of 0 that’s working against you. Try adjusting your loop to something like this:

for i = 1, ellipse.n do local j = i - 1 a = 2\*math.pi\*j/ellipse.n ellipse.vertices[2\*i - 1] = ellipse.rx\*math.cos(a) ellipse.vertices[2\*i] = -ellipse.ry\*math.sin(a) print("Polygon : X" .. j .. "/Y" .. j .. " : " .. ellipse.vertices[2\*i-1] .. " ; " .. ellipse.vertices[2\*i]) end

Hi,

Thank you very much StarCrunch, you solved my problem. I’m not a Lua dev more a C/C++ dev… I feel like a noobie with this language!

Hi.

You have an index of 0 that’s working against you. Try adjusting your loop to something like this:

for i = 1, ellipse.n do local j = i - 1 a = 2\*math.pi\*j/ellipse.n ellipse.vertices[2\*i - 1] = ellipse.rx\*math.cos(a) ellipse.vertices[2\*i] = -ellipse.ry\*math.sin(a) print("Polygon : X" .. j .. "/Y" .. j .. " : " .. ellipse.vertices[2\*i-1] .. " ; " .. ellipse.vertices[2\*i]) end

Hi,

Thank you very much StarCrunch, you solved my problem. I’m not a Lua dev more a C/C++ dev… I feel like a noobie with this language!