# triangulate function

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

``````

My comments here and here may be of interest.

@mpilgrem

That’s where I ended up after doing several google and then forum searches for information on triangulate. Once I read them and changed the table order, the function worked fine and I was able to finish my program. The problem was that I was using the Codea documentation that didn’t mention anything about the order of the vec2 values in the table. Since I hadn’t used the triangulate function before, I wasn’t sure why it wasn’t working or if I just didn’t understand it. That’s how I learn new functions. I’ll write small programs that use them so I can see how they work. I know that it’s hard to keep documentation updated, because when I was working the documentation was the last thing that got done, if at all. It would have saved me a lot of frustration if I would have read you comments before I started. I guess a forum search should be the first place to start when something doesn’t work the way you expect it to.

I have added the gap in the in-app reference and a proposal that the `triangulate()` function should be enhanced to be indifferent to the order of the input points to the Issue Tracker (as items #176 and #177 respectively).