Catch a failed math.triangulate without crashing?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Connorses
Citizen
Posts: 63
Joined: Fri Sep 06, 2013 7:02 am

Catch a failed math.triangulate without crashing?

Post by Connorses »

This one's pretty straightforward. I would like to be able to call triangulate without the game crashing if it fails. Failing that, I'd like to be able to test whether my points can be triangulated or not (I want the user to be able to draw polygons without crashing the game).
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Catch a failed math.triangulate without crashing?

Post by pgimeno »

Doesn't pcall() help?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Catch a failed math.triangulate without crashing?

Post by ivan »

Connorses wrote: Tue Aug 18, 2020 9:41 pm This one's pretty straightforward. I would like to be able to call triangulate without the game crashing if it fails. Failing that, I'd like to be able to test whether my points can be triangulated or not
Make sure you are working with simple polygons:
https://2dengine.com/?p=polygons#Simple_or_complex
Connorses wrote: Tue Aug 18, 2020 9:41 pmFailing that, I'd like to be able to test whether my points can be triangulated or not
Yes, you can use pcall to check if the function raises an error.
But note that Love2D's triangulation function is not efficient and the quality of triangulation is poor.
I think love.math.triangulate uses "ear clipping" as described in my tutorial:
https://2dengine.com/?p=polygons#Triangulation
This is a simple technique but it's slow and produces long skinny triangles.
Connorses
Citizen
Posts: 63
Joined: Fri Sep 06, 2013 7:02 am

Re: Catch a failed math.triangulate without crashing?

Post by Connorses »

Thanks for the help! I wound up using a pcall to catch it.
Perhaps I'll look into other triangulate options in the future, but for now Love's seems to work.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 148 guests