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
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:
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.
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.
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))
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.)
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.