Overlay Polygons Using Set Logic

This example shows how to overlay polygons using set logic. The polybool function can perform standard set operations, such as intersection, union, subtraction, and exclusive OR, on two sets of vectors, which can represent x-y or latitude-longitude coordinate pairs. For more information, see Polygon Set Logic.

Create Two Polygons

To illustrate these set operations, create a 12-sided polygon and a triangle that overlaps it.

az = (0:pi/6:2*pi)';
lat1 = cos(az);
lon1 = sin(az);

lat2 = [0 1 -1 0]';
lon2 = [0 2 2 0]';

Compute the Intersection of the Two Polygons

Plot the two shapes together with blue and red lines. Compute the intersection polygon using polybool and plot it using geoshow.

figure
axesm miller
plotm(lat1,lon1,'b')
plotm(lat2,lon2,'r')
[loni,lati] = polybool('intersection',lon1,lat1,lon2,lat2);
[lati loni]
ans = 5×2

    0.0000    1.0000
   -0.4409    0.8819
    0.0000         0
    0.4409    0.8819
    0.0000    1.0000

geoshow(lati,loni,'DisplayType','polygon')

Compute the Union of the Two Polygons

Plot the two shapes together with blue and red lines. Compute the union polygon using polybool and plot it using geoshow.

figure
axesm miller
plotm(lat1,lon1,'b')
plotm(lat2,lon2,'r')
[lonu,latu] = polybool('union',lon1,lat1,lon2,lat2);
[latu lonu]
ans = 16×2

   -1.0000    2.0000
   -0.4409    0.8819
   -0.5000    0.8660
   -0.8660    0.5000
   -1.0000    0.0000
   -0.8660   -0.5000
   -0.5000   -0.8660
    0.0000   -1.0000
    0.5000   -0.8660
    0.8660   -0.5000
      ⋮

geoshow(latu,lonu,'DisplayType','polygon')

Compute the Exclusive-Or of the Two Polygons

Plot the two shapes together with blue and red lines. Compute the Exclusive-Or polygon using polybool and plot it using geoshow.

figure
axesm miller
plotm(lat1,lon1,'b')
plotm(lat2,lon2,'r')
[lonx,latx] = polybool('xor',lon1,lat1,lon2,lat2);
[latx lonx]
ans = 22×2

   -1.0000    2.0000
   -0.4409    0.8819
   -0.5000    0.8660
   -0.8660    0.5000
   -1.0000    0.0000
   -0.8660   -0.5000
   -0.5000   -0.8660
    0.0000   -1.0000
    0.5000   -0.8660
    0.8660   -0.5000
      ⋮

geoshow(latx,lonx,'DisplayType','polygon')

Compute the Subtraction of the Two Polygons

Plot the two shapes together with blue and red lines. Subtract the Exclusive-Or triangle from the 12-sided polygon and plot the resulting concave polygon using geoshow.

figure
axesm miller
plotm(lat1,lon1,'b')
plotm(lat2,lon2,'r')
[lonm,latm] = polybool('minus',lon1,lat1,lon2,lat2);
[latm lonm]
ans = 15×2

    0.8660    0.5000
    0.5000    0.8660
    0.4409    0.8819
    0.0000         0
   -0.4409    0.8819
   -0.5000    0.8660
   -0.8660    0.5000
   -1.0000    0.0000
   -0.8660   -0.5000
   -0.5000   -0.8660
      ⋮

geoshow(latm,lonm,'DisplayType','polygon')