Page 1 of 1

isTriangulation function?

Posted: Wed Jan 08, 2014 4:20 pm
by Maxwell
Love2D supplies a triangulation function that you input coordinates of multiple points and it'll return a table filled with coordinates for triangles. However if you input coordinates that aren't triangulable it will error out (returning exceptions). I don't know if there's a way around it other than adding another function to Love2D such as "isTriangulation" like the "isConvex" where it will return false if it runs into an exception and true if all is successful.

So what should I do if I want to be able to see if points are triangulable before triangulating it? Thanks this will be very helpful for me and my project if I find a way :D

Re: isTriangulation function?

Posted: Wed Jan 08, 2014 7:05 pm
by Roland_Yonaba
Actually, you can try something simple, use pcall.

Pcall calls a Lua function in a safe/protective manner, and will tell you back whether or not this function produces an error, without affecting the flow of your application/game. Assuming polygon refers to a table containing the coordinates of the vertices of the polygon to be tested for triangulation:

Code: Select all

local ok, err = pcall(love.math.triangulate, polygon)
if not ok then
  print('Could not triangulate polygon. Error reported was:\n ' .. err)
end

Re: isTriangulation function?

Posted: Wed Jan 08, 2014 9:41 pm
by Maxwell
Oh wow! Thank you Roland very very useful :)!

Not to bother you further but do you know of a way to check a polygons size by just knowing it's points? I basically want to restrict too small of polygons or too large for Box2D to handle before they are created.

Re: isTriangulation function?

Posted: Wed Jan 08, 2014 11:02 pm
by Roland_Yonaba
Maxwell wrote:do you know of a way to check a polygons size by just knowing it's points? I basically want to restrict too small of polygons or too large for Box2D to handle before they are created.

Code: Select all

local function polygon_area(points)
  local area = 0
  local p1 = points[1]
  for i = 2, #points - 1 do
    local pA, pB = points[i], points[i+1]
    local x1, y1 = pA.x - p1.x, pA.y - p1.y
    local x2, y2 = pB.x - p1.x,  pB.y - p1.y
    area = area + (x1 * y2 - y1 * x2)
  end
  return math.abs (area/2)
end

-- Example:
local points = {{x=0,y=0},{x=2,y=0},{x=1,y=1}}
print(polygon_area(points))
:awesome:

Re: isTriangulation function?

Posted: Wed Jan 08, 2014 11:37 pm
by Maxwell
thank you! you are a savior :D I should of paid more attention in math class lol!

Re: isTriangulation function?

Posted: Thu Jan 09, 2014 8:54 am
by Plu
Perhaps worhtwhile to note (as explained in the documentation) that the first return value is a boolean, but the second one is either an error message or the first result returned by the function called. So you can also use it to get the result from the call. (The "err" name of the variable made me think that p_call doesn't actually run the function and instead only tests if it would run, but this is not the case.)

Re: isTriangulation function?

Posted: Thu Jan 09, 2014 3:02 pm
by Roland_Yonaba
Plu wrote:Perhaps worhtwhile to note (as explained in the documentation) that the first return value is a boolean, but the second one is either an error message or the first result returned by the function called. So you can also use it to get the result from the call. (The "err" name of the variable made me think that p_call doesn't actually run the function and instead only tests if it would run, but this is not the case.)
Perfectly right, thanks Plu. I guess that is the reason for what I mentionned the link to the documentation. ;)