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
isTriangulation function?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: isTriangulation function?
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:
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?
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.
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.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: isTriangulation function?
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))
Re: isTriangulation function?
thank you! you are a savior I should of paid more attention in math class lol!
Re: isTriangulation function?
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.)
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: isTriangulation function?
Perfectly right, thanks Plu. I guess that is the reason for what I mentionned the link to the documentation.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.)
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 5 guests