I have found several solutions for crazy polygon detecting algorithms, however, my problem is a bit simpler. I have a line created by an array of indexed points. Each click on map creates a new point and appends it to the end of the array, and creates a new line from the previous end to the new one.
I would like to detect when you create a point in a way that the line folds on itself and creates a polygon. I currently create a polygon when you click the first point again. Then all the points inside get selected.
Here the 4th point was created and the line closed creating a triangle. Is there a simple solution to detect this?
Edit: Using leaflet library