({{, (RefLink[RegionDifference, paclet:ref/RegionDifference])[(Subscript[reg, 1]),(Subscript[reg, 2])] 
gives the difference of the regions (Subscript[reg, 1]) and (Subscript[reg, 2]).}})

OpenersNotesSection(Spacer1)Details and Options

A point p belongs to (RefLink[RegionDifference, paclet:ref/RegionDifference])[(Subscript[reg, 1]),(Subscript[reg, 2])] if it belongs to (Subscript[reg, 1]) but not (Subscript[reg, 2]).

For (RefLink[BoundaryMeshRegion, paclet:ref/BoundaryMeshRegion]) (Subscript[reg, 1]) and (Subscript[reg, 2]), (RefLink[RegionDifference, paclet:ref/RegionDifference]) gives the smallest (RefLink[BoundaryMeshRegion, paclet:ref/BoundaryMeshRegion]) that contains the difference of (Subscript[reg, 1]) and (Subscript[reg, 2]).

For (RefLink[MeshRegion, paclet:ref/MeshRegion]) (Subscript[reg, 1]) and (Subscript[reg, 2]), (RefLink[RegionDifference, paclet:ref/RegionDifference]) gives the smallest (RefLink[MeshRegion, paclet:ref/MeshRegion]) that contains the difference of (Subscript[reg, 1]) and (Subscript[reg, 2]).

(RefLink[RegionDifference, paclet:ref/RegionDifference]) takes the same options as (RefLink[Region, paclet:ref/Region]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (25)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Difference of two disks:

RegionDifference[Disk[{0,0},2],Disk[{3,0},2]];

Visualize it:

Region[%]

Difference of two (RefLink[MeshRegion, paclet:ref/MeshRegion]) objects:

RegionDifference[,]

OpenersExampleSection1(Spacer1)Scope  (12)

OpenersExampleSubsection0(Spacer1)Special Regions  (6)

For some regions, the difference is computed explicitly:

(Subscript[ℛ, 1])=Triangle[{{0,0},{1,0},{0,1}}];
(Subscript[ℛ, 2])=Disk[{3,0},1];

(Subscript[ℛ, 3])=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]

The regions are disjoint:

Graphics[{Opacity[0.5],{Red,(Subscript[ℛ, 1])},{Green,(Subscript[ℛ, 2])},{Yellow,(Subscript[ℛ, 3])}}]

Here the difference is empty:

(Subscript[ℛ, 1])=Cuboid[];
(Subscript[ℛ, 2])=Ball[{0,0,0},2];

(Subscript[ℛ, 3])=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]

The cuboid is contained in the ball:

Graphics3D[{Opacity[0.5],{Red,(Subscript[ℛ, 1])},{Green,(Subscript[ℛ, 2])}}]

A difference of (RefLink[Line, paclet:ref/Line]) regions:

ℛ=RegionDifference[Line[{{1},{4}}],Line[{{2},{3}}]];

Visualize it:

Region[ℛ]

A difference of (RefLink[Polygon, paclet:ref/Polygon]) regions:

(Subscript[ℛ, 1])=Polygon[{{0,0},{3,-1},{2,0},{3,1}}];
(Subscript[ℛ, 2])=Polygon[{{5,0},{2,1},{3,0},{2,-1}}];

ℛ=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

Visualize it:

Region[ℛ]

A difference of two (RefLink[Cuboid, paclet:ref/Cuboid]) regions:

ℛ=RegionDifference[Cuboid[],Cuboid[{0.5,0.5,0.5}]];

Visualize it:

Region[ℛ]

A difference of regions with different (RefLink[RegionDimension, paclet:ref/RegionDimension]):

ℛ=RegionDifference[Circle[{0,0},1],Disk[{1,0},1]];

Visualize it:

Region[ℛ]

OpenersExampleSubsection1(Spacer1)Formula Regions  (2)

A difference of (RefLink[ImplicitRegion, paclet:ref/ImplicitRegion]) objects is an (RefLink[ImplicitRegion, paclet:ref/ImplicitRegion]):

(Subscript[ℛ, 1])=ImplicitRegion[x≤1,{x}];
(Subscript[ℛ, 2])=ImplicitRegion[x≥-1,{x}];

RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]

2D:

(Subscript[ℛ, 1])=ImplicitRegion[(x^2)+(y^2)≤1,{x,y}];
(Subscript[ℛ, 2])=ImplicitRegion[(x^2)+(((y-1))^2)≤1,{x,y}];

RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]

3D:

(Subscript[ℛ, 1])=ImplicitRegion[(x^2)+(y^2)+(z^2)≤1,{x,y,z}];
(Subscript[ℛ, 2])=ImplicitRegion[(((x-1))^2)+(y^2)+(z^2)≤1,{x,y,z}];

RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]

nD:

(Subscript[ℛ, 1])=ImplicitRegion[(x^2)+(y^2)+(z^2)+(u^2)+(v^2)≤1,{x,y,z,u,v}];
(Subscript[ℛ, 2])=ImplicitRegion[(((x-1))^2)+(y^2)+(z^2)+(u^2)+(v^2)≤1,{x,y,z,u,v}];

RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]

A difference of (RefLink[ParametricRegion, paclet:ref/ParametricRegion]) objects:

(Subscript[ℛ, 1])=ParametricRegion[{u,v},{{u,0,2},{v,0,2}}];
(Subscript[ℛ, 2])=ParametricRegion[{u+1,v+1},{{u,0,2},{v,0,2}}];

ℛ=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

Visualize it:

Region[ℛ]

OpenersExampleSubsection2(Spacer1)Mesh Regions  (2)

A difference of (RefLink[BoundaryMeshRegion, paclet:ref/BoundaryMeshRegion]) objects is a (RefLink[BoundaryMeshRegion, paclet:ref/BoundaryMeshRegion]):

RegionDifference[,]

BoundedRegionQ[%]

2D:

RegionDifference[,]

BoundedRegionQ[%]

3D:

RegionDifference[,]

BoundedRegionQ[%]

A difference of full-dimensional (RefLink[MeshRegion, paclet:ref/MeshRegion]) objects is a (RefLink[MeshRegion, paclet:ref/MeshRegion]):

RegionDifference[,]

MeshRegionQ[%]

2D:

RegionDifference[,]

MeshRegionQ[%]

3D:

RegionDifference[,]

MeshRegionQ[%]

OpenersExampleSubsection3(Spacer1)Derived Regions  (2)

A difference of (RefLink[BooleanRegion, paclet:ref/BooleanRegion]) objects:

(Subscript[ℛ, 1])= BooleanRegion[Or,{Triangle[{{0,0},{2,3},{-2,3}}],Triangle[{{0,2},{2,-1},{-2,-1}}]}];
(Subscript[ℛ, 2]) = BooleanRegion[And,{Triangle[{{0,0},{2,3},{-2,3}}],Triangle[{{0,2},{2,-1},{-2,2}}]}];

ℛ=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

Visualize it:

Region[ℛ]

A difference of (RefLink[TransformedRegion, paclet:ref/TransformedRegion]) objects:

(Subscript[ℛ, 1])=TransformedRegion[Cuboid[],RotationTransform[Pi/8,{1,0,0}]];
(Subscript[ℛ, 2])=TransformedRegion[Cuboid[],RotationTransform[Pi/8,{0,1,0}]];

ℛ=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

Visualize it:

Region[ℛ]

OpenersExampleSection3(Spacer1)Applications  (4)

Difference of regions:

{(Subscript[ℛ, 1]),(Subscript[ℛ, 2])}={,};

Multicolumn[Table[RegionDifference[(Subscript[ℛ, 1]),TransformedRegion[(Subscript[ℛ, 2]),TranslationTransform[{0,0,t}]]],{t,0,2.5,0.5}],3,Appearance->"Horizontal" ]

Define a disk annulus as the difference of two disks:

annulus=RegionDifference[Disk[{0,0},2],Disk[{0,0},1]];

Region[annulus]

The area is the difference of areas:

Area[annulus]

Area[Disk[{0,0},2]]-Area[Disk[{0,0},1]]

Define a ball shell (sometimes called spherical shell) as the generalization of an annulus to 3D:

shell=RegionDifference[Ball[{0,0,0},2],Ball[{0,0,0},1]];

RegionPlot3D[shell,PlotPoints->40,PlotStyle->Opacity[0.5]]

The volume is the difference of volumes:

Volume[shell]

Volume[Ball[{0,0,0},2]]-Volume[Ball[{0,0,0},1]]

Create illusory contours:

disks=RegionUnion[Disk[{0,0},2],Disk[{6,0},2],Disk[{3,5},2]];

RegionDifference[disks,Triangle[{{0,0},{6,0},{3,5}}]]//Region

OpenersExampleSection4(Spacer1)Properties & Relations  (5)

A point p belongs to (RefLink[RegionDifference, paclet:ref/RegionDifference])[(Subscript[reg, 1]),(Subscript[reg, 2])] if it belongs to (Subscript[reg, 1]) but not (Subscript[reg, 2]):

(Subscript[ℛ, 1])=Disk[{0,0},2];
(Subscript[ℛ, 2])=Disk[{0,3},2];
(Subscript[ℛ, 3])=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

Use (RefLink[RegionMember, paclet:ref/RegionMember]) to test membership:

p={0,0};

RegionMember[(Subscript[ℛ, 3]), p] == RegionMember[(Subscript[ℛ, 1]),p]∧ !RegionMember[(Subscript[ℛ, 2]), p]

(RefLink[RegionDifference, paclet:ref/RegionDifference]) is a Boolean combination ¬#2∧#1 of two regions:

{(Subscript[ℛ, 1]),(Subscript[ℛ, 2])} = {,};

RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]==BooleanRegion[¬#2∧#1&,{(Subscript[ℛ, 1]),(Subscript[ℛ, 2])}]

(RefLink[RegionSymmetricDifference, paclet:ref/RegionSymmetricDifference])  can be found using (RefLink[RegionUnion, paclet:ref/RegionUnion]) and (RefLink[RegionDifference, paclet:ref/RegionDifference]):

{(Subscript[ℛ, 1]),(Subscript[ℛ, 2])} = {,};

RegionSymmetricDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]== RegionUnion[RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])],RegionDifference[(Subscript[ℛ, 2]),(Subscript[ℛ, 1])]]

The (RefLink[RegionDimension, paclet:ref/RegionDimension]) of a difference is at most that of the first input:

(Subscript[ℛ, 1])=Disk[{0,0},1];
(Subscript[ℛ, 2])=Disk[{1,0},1];
(Subscript[ℛ, 3])=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

RegionDimension[(Subscript[ℛ, 3])]==RegionDimension[(Subscript[ℛ, 1])]

It can be less, however:

(Subscript[ℛ, 1])=Disk[{0,0},1];
(Subscript[ℛ, 2])=ImplicitRegion[y>0∨y<0,{x,y}];
(Subscript[ℛ, 3])=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

This difference is a line segment, and thus has dimension 1:

RegionDimension[(Subscript[ℛ, 3])]

If two regions are disjoint, the (RefLink[RegionMeasure, paclet:ref/RegionMeasure]) of their difference is that of the first input:

(Subscript[ℛ, 1])=Disk[{0,0},1];
(Subscript[ℛ, 2])=Disk[{3,0},1];
(Subscript[ℛ, 3])=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

RegionMeasure[(Subscript[ℛ, 3])]==RegionMeasure[(Subscript[ℛ, 1])]

If they overlap, you must subtract the measure of the (RefLink[RegionIntersection, paclet:ref/RegionIntersection]):

(Subscript[ℛ, 1])=Disk[{0,0},1];
(Subscript[ℛ, 2])=Disk[{1,0},1];
(Subscript[ℛ, 3])=RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])];

RegionMeasure[(Subscript[ℛ, 3])]==RegionMeasure[(Subscript[ℛ, 1])]-RegionMeasure[RegionIntersection[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

Difference is defined only for regions with the same (RefLink[RegionEmbeddingDimension, paclet:ref/RegionEmbeddingDimension]):

(Subscript[ℛ, 1])=Disk[];
(Subscript[ℛ, 2])=Ball[{0,0,1},1];

RegionDifference[(Subscript[ℛ, 1]),(Subscript[ℛ, 2])]

OpenersExampleSection6(Spacer1)Neat Examples  (1)

The difference of two spiral polygons:

RegionDifference[,]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[RegionUnion, paclet:ref/RegionUnion])  ▪  (RefLink[RegionIntersection, paclet:ref/RegionIntersection])  ▪  (RefLink[RegionSymmetricDifference, paclet:ref/RegionSymmetricDifference])  ▪  (RefLink[BooleanRegion, paclet:ref/BooleanRegion])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Derived Geometric Regions, paclet:guide/DerivedRegions])}, {▪(RefLinkPlain[Polygons, paclet:guide/Polygons])}, {▪(RefLinkPlain[Polyhedra, paclet:guide/Polyhedra])}})}})