sortboundaries

Reorders the boundaries of a polyshape by a chosen criterion.

Category: Mathematics > Computational Geometry

Syntax

pshape = sortboundaries(pshape, criterion)

Sorts the boundaries in ascending order of the criterion.

pshape = sortboundaries(pshape, criterion, direction)

Sorts in 'ascend' or 'descend' order.

Description

Sorting changes only the order in which boundaries are stored, not the shape. That order is what rmboundary, ishole and the index arguments of area and perimeter refer to, so sorting first makes them select 'the largest' or 'the smallest' piece reliably.

Input arguments

criterion
What to sort by: 'area', 'perimeter', 'numsides' or 'centroid'.
direction
'ascend' (default) or 'descend'.

Output arguments

pshape
The polyshape with the same geometry and boundaries in the new order, which is the order of Vertices.

Example

Largest piece first

P = polyshape([0 1 1 0 NaN 3 5 5 3], [0 0 1 1 NaN 0 0 2 2]);
S = sortboundaries(P, 'area', 'descend');
S.Vertices
Output
ans = 9×2

     3     0
     3     2
     5     2
     5     0
   NaN   NaN
     0     0
     0     1
     1     1
     1     0

See also

sortregions rmboundary ishole numboundaries