Main Content

xor

Exclusive OR of two polyshape objects

Description

polyout = xor(poly1,poly2) returns a polyshape object whose regions are the geometric exclusive OR of two polyshape objects. The geometric exclusive OR contains the regions of poly1 and poly2 that do not overlap. poly1 and poly2 must have compatible array sizes.

example

[polyout,shapeID,vertexID] = xor(poly1,poly2) also returns vertex mapping information from the vertices in polyout to the vertices in poly1 and poly2. The xor function only supports this syntax when poly1 and poly2 are scalar polyshape objects.

The shapeID elements identify whether the corresponding vertex in polyout originated in poly1, poly2, or was created from the exclusive OR. vertexID maps the vertices of polyout to the vertices of poly1, poly2, or the exclusive OR.

example

___ = xor(___,Name=Value) specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. You can use any of the output argument combinations in previous syntaxes. For example, polyout = xor(poly1,poly2,Simplify=false) returns a polyshape object whose vertices have not been modified regardless of intersections or improper nesting.

Examples

collapse all

Create and plot two polygons.

poly1 = polyshape([0 0 1 1],[1 0 0 1]);
poly2 = polyshape([0.75 1.25 1.25 0.75],[0.25 0.25 0.75 0.75]);
plot(poly1)
hold on
plot(poly2)

Figure contains an axes object. The axes object contains 2 objects of type polygon.

figure

Compute and plot the exclusive OR of poly1 and poly2.

polyout = xor(poly1,poly2)
polyout = 
  polyshape with properties:

      Vertices: [13x2 double]
    NumRegions: 2
      NumHoles: 0

plot(polyout)

Figure contains an axes object. The axes object contains an object of type polygon.

Create two polygons, and compute and plot their exclusive OR. Display the vertex coordinates of the exclusive OR and the corresponding vertex mapping information.

poly1 = polyshape([0 0 1 1],[1 0 0 1]);
poly2 = translate(poly1,[0.5 0]);
[polyout,shapeID,vertexID] = xor(poly1,poly2);
plot(polyout)

Figure contains an axes object. The axes object contains an object of type polygon.

[polyout.Vertices shapeID vertexID]
ans = 9×4

         0    1.0000    1.0000    1.0000
    0.5000    1.0000    2.0000    1.0000
    0.5000         0    2.0000    4.0000
         0         0    1.0000    4.0000
       NaN       NaN       NaN       NaN
    1.0000    1.0000    1.0000    2.0000
    1.5000    1.0000    2.0000    2.0000
    1.5000         0    2.0000    3.0000
    1.0000         0    1.0000    3.0000

There are two boundaries in the exclusive OR, separated by a row of NaN values in the Vertices property. For example, consider the first boundary in the array. The first and last vertices of the exclusive OR originated in poly1, since the corresponding values in shapeID are 1. These vertices are the first and fourth vertices in the property poly1.Vertices, respectively, since the corresponding values in vertexID are 1 and 4. Similarly, the second and third vertices of the exclusive OR originated in poly2, and they are also the first and fourth vertices in the property poly2.Vertices, respectively.

Input Arguments

collapse all

First input polyshape, specified as a scalar, vector, matrix, or multidimensional array.

Second input polyshape, specified as a scalar, vector, matrix, or multidimensional array.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: polyout = xor(poly1,poly2,Simplify=false)

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: polyout = xor(poly1,poly2,"Simplify",false)

Keep collinear points as vertices, specified as one of these numeric or logical values:

  • 1 (true) — Keep all collinear points as vertices.

  • 0 (false) — Remove collinear points so that the output polyshape contains the fewest vertices necessary to define the boundaries.

If you do not specify the KeepCollinearPoints name-value argument, the function assigns its value according to the values used during creation of the input polyshape objects.

  • If each input polyshape kept collinear points as vertices during creation, then the function sets KeepCollinearPoints to true.

  • If each input polyshape removed collinear points during creation, then the function sets KeepCollinearPoints to false.

  • If the collinear points of the input polyshape objects were treated differently, then the function sets KeepCollinearPoints to false.

Modify polygon vertices to simplify output, specified as one of these numeric or logical values:

  • 1 (true) — Modify polygon vertices to produce a well-defined polygon when the output vertices produce intersections or improper nesting.

  • 0 (false) — Produce a polygon that may contain intersecting edges, improper nesting, duplicate points, or degeneracies. Computing with ill-defined polygons can lead to inaccurate or unexpected results.

Output Arguments

collapse all

Output polyshape, returned as a scalar, vector, matrix, or multidimensional array.

The two input polyshape arguments must have compatible sizes. For example, if two input polyshape vectors have different lengths M and N, then they must have different orientations (one must be a row vector and one must be a column vector). polyout is then M-by-N or N-by-M depending on the orientation of each input vector. For more information on compatible array sizes, see Compatible Array Sizes for Basic Operations.

Shape ID, returned as a column vector whose elements each represent the origin of the vertex in the exclusive OR. The value of an element in shapeID is 0 when the corresponding vertex of the output polyshape was created by the exclusive OR. An element is 1 when the corresponding vertex originated from poly1, and 2 when it originated from poly2.

The length of shapeID is equal to the number of rows in the Vertices property of the output polyshape. The xor function only supports this output argument if the input polyshape objects are scalar.

Data Types: double

Vertex ID, returned as a column vector whose elements map the vertices in the output polyshape to the vertices in the polyshape of origin. The elements of vertexID contain the row numbers of the corresponding vertices in the Vertices property of the input polyshape. An element is 0 when the corresponding vertex of the output polyshape was created by the exclusive OR.

The length of vertexID is equal to the number of rows in the Vertices property of the output polyshape. The xor function only supports this output argument when the input polyshape objects are scalar.

Data Types: double

Extended Capabilities

Version History

Introduced in R2017b

expand all