Sort points, flip matrix inside a boundary, then merge trees

You could use that