I tried playing with the Codea triangulate function, but at first I couldn’t get it to return a table of points. I passed it a table of vec2 points in the form of a polygon in a counter clockwise direction. I kept thinking I was doing something wrong, but It wasn’t until I did some searches that I found the problem. The triangulate function only work correctly if the table of points are in a clockwise direction. Triangular points for meshes have to be created in a counter clockwise direction, so it seems that the polygon points for triangulate would also be in a counter clockwise direction (NOT). So, here is a program that shows how the triangulate function breaks up a polygon. Keep touching the screen in a clockwise direction to create points of a polygon. The triangulate function will return a table of points of a triangle that I draw as you keep creating more points. If you change to a counter clockwise direction, triangulate will start giving incorrect but interesting results. @Simeon, could you update the documentation to include information that is important for a function to work correctly.
-- Example of the Codea triangulate function.
-- triangulate breaks a polygon up into triangles.
-- triangulate works going in a clockwise direction.
-- switching to a counter clockwise direction
-- gives unpredictable but interesting results.
function setup()
displayMode(FULLSCREEN)
t1={}
t2={}
end
function draw()
background(40,40,40)
fill(255)
fontSize(20)
text("Example of the Codea triangulate function.",WIDTH/2,HEIGHT-50)
text("Keep touching the screen to create a polygon point.",WIDTH/2,HEIGHT-75)
for a,b in pairs(t1) do
ellipse(b.x,b.y,2,2)
str=string.format("(%d,%d)",b.x,b.y)
fontSize(12)
text(str,b.x,b.y-10)
end
lines()
end
function touched(t)
if t.state==BEGAN then
table.insert(t1,#t1+1,vec2(t.x,t.y))
t2=triangulate(t1)
end
end
function lines()
if t2~=nil then
stroke(255)
strokeWidth(2)
for a=1,#t2,3 do
line(t2[a].x,t2[a].y,t2[a+1].x,t2[a+1].y)
line(t2[a+1].x,t2[a+1].y,t2[a+2].x,t2[a+2].y)
line(t2[a+2].x,t2[a+2].y,t2[a].x,t2[a].y)
end
end
end