RegionWithin[reg1,reg2]
returns True if reg2 is contained within reg1.
RegionWithin
RegionWithin[reg1,reg2]
returns True if reg2 is contained within reg1.
Details and Options
- The region reg2 is contained within reg1 if every point that belongs to reg2 also belongs to reg1.
- If all regi are parameter-free regions, i.e. ConstantRegionQ[regi] is True, the regions are point sets, and typically True or False is returned.
- If some regi depend on parameters, i.e. ConstantRegionQ[regi] is False, then regi represents a family of regions, and RegionWithin will attempt to compute conditions on parameters such that reg2 is contained within reg1.
- The following options can be given:
-
Assumptions $Assumptions assumptions to make about parameters GenerateConditions False whether to generate conditions on parameters
Examples
open all close allBasic Examples (2)
Test whether a region is contained within another:
Subscript[ℛ, 1] = Disk[];
Subscript[ℛ, 2] = Rectangle[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Graphics[{Opacity[0.5], {Red, Subscript[ℛ, 2]}, {Green, Subscript[ℛ, 1]}}]Generate conditions for which a region is contained within another:
Subscript[ℛ, 1] = ImplicitRegion[Subsuperscript[r, 1, 2] ≤ x^2 + y^2 ≤ Subsuperscript[r, 2, 2], {x, y}];
Subscript[ℛ, 2] = Annulus[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Scope (15)
Basic Uses (3)
Subscript[ℛ, 1] = Disk[];
Subscript[ℛ, 2] = Triangle[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Graphics[{Opacity[0.5], {Red, Subscript[ℛ, 2]}, {Green, Subscript[ℛ, 1]}}]Show a region is not within another:
Subscript[ℛ, 1] = Parallelogram[];
Subscript[ℛ, 2] = Circle[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Graphics[{Thick, Opacity[0.5], {Red, Subscript[ℛ, 2]}, {Green, Subscript[ℛ, 1]}}]Find conditions that make a region a subset of another:
Subscript[ℛ, 1] = Disk[{0, 0}, r];
Subscript[ℛ, 2] = Parallelogram[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Basic Regions (4)
Regions in
including Line and Interval:
RegionWithin[Interval[{-3, 1}], Line[{{0}, {1}}]]RegionWithin[Interval[{2, 2}], Point[{2}]]Ball:
RegionWithin[Interval[{-2, 0}], Ball[1]]RegionWithin[InfiniteLine[{-3}, {1}], InfiniteLine[{0}, {1}]]Regions in
including Point:
Subscript[ℛ, 1] = Point[Tuples[Range[3], 2]];
Subscript[ℛ, 2] = Point[Tuples[Range[2], 2]];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Line:
Subscript[ℛ, 1] = Line[{{7, -1}, {6, -1}, {5, -1}, {3, 3}, {1, 1}, {0, 0}}];
Subscript[ℛ, 2] = Line[{{0, 0}, {3, 3}, {5, -1}}];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Graphics[{Thick, Opacity[0.5], {Red, Subscript[ℛ, 2]}, {Green, Subscript[ℛ, 1]}}]Subscript[ℛ, 1] = Triangle[{{-1, -1}, {1, -1}, {1, 1}}];
Subscript[ℛ, 2] = Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}}];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Graphics[{Opacity[0.5], {Red, Subscript[ℛ, 2]}, {Green, Subscript[ℛ, 1]}}]Subscript[ℛ, 1] = Ellipsoid[{0, 0}, {3, 2}];
Subscript[ℛ, 2] = Disk[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Rectangle and RegularPolygon:
RegionWithin[RegularPolygon[r, 6], Rectangle[]]Regions in
including Point:
Subscript[ℛ, 1] = Point[Tuples[Range[5], 3]];
Subscript[ℛ, 2] = Point[Tuples[Range[3], 3]];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Line:
Subscript[ℛ, 1] = Line[{{7, -1, 1}, {5, -1, 0}, {0, 0, 0}}];
Subscript[ℛ, 2] = Line[{{0, 0, 0}, {3, 3, 3}, {5, -1, 0}, {7, -1, 1}}];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = Triangle[{{2, 0, 0}, {0, 0, 2}, {0, 2, 2}}];
Subscript[ℛ, 2] = Polygon[{{1, 0, 1}, {0, 1, 2}, {1, 1, 1}, {2, 0, 0}}];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Graphics3D[{Opacity[0.5], {Red, Subscript[ℛ, 2]}, {Green, Subscript[ℛ, 1]}}, Boxed -> False]Cuboid and Hexahedron:
Subscript[ℛ, 1] = Hexahedron[{{0, 0, 0}, {1, 0, 0}, {2, 1, 0}, {1, 1, 0}, {0, 0, 1}, {1, 0, 1}, {2, 1, 1}, {1, 1, 1}}];
Subscript[ℛ, 2] = Cuboid[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = Ellipsoid[{0, 0, 0}, {3, 2, 1}];
Subscript[ℛ, 2] = Ball[3];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Tetrahedron and Simplex:
Subscript[ℛ, 1] = Simplex[{{1, 0, 1}, {1, 0, 0}, {0, 0, 1}, {1, 1, 1}}];
Subscript[ℛ, 2] = Tetrahedron[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Regions in
including Cuboid and Parallelepiped in
:
Subscript[ℛ, 1] = Parallelepiped[{-1, -1, -1, -1}, 2IdentityMatrix[4]];
Subscript[ℛ, 2] = Cuboid[{0, 0, 0, 0}, {1, 1, 1, 1}];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = Ball[10];
Subscript[ℛ, 2] = Ellipsoid[Table[0, 10], Table[i, {i, 10}]];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Formula Regions (4)
Subscript[ℛ, 1] = ImplicitRegion[x^2 + y^2 ≤ 1, {x, y}];
Subscript[ℛ, 2] = ImplicitRegion[1 / 2 ≤ x^2 + y^2 ≤ 1, {x, y}];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = ParametricRegion[{r Cos[θ], r Sin[θ]}, {{r, 0, 2}, {θ, 0, 2π}}];
Subscript[ℛ, 2] = ParametricRegion[{x y, x - y}, {{x, 0, 1}, {y, 0, 1}}];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = ParametricRegion[{ρSqrt[4 - u^2]Cos[θ], ρSqrt[4 - u^2]Sin[θ], ρ u}, {{ρ, 0, 1}, {θ, 0, 2π}, {u, -2, 2}}];
Subscript[ℛ, 2] = ImplicitRegion[x^4 + y^4 + z^4 ≤ 1, {x, y, z}];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]RegionWithin[ImplicitRegion[Subsuperscript[r, 1, 2] ≤ x^2 + y^2 ≤ Subsuperscript[r, 2, 2], {x, y}], Annulus[]]Mesh Regions (3)
Compare MeshRegion in
:
Subscript[ℛ, 1] = MeshRegion[{{-1}, {2}}, Line[{1, 2}]];
Subscript[ℛ, 2] = MeshRegion[{{0}, {1}}, Line[{1, 2}]];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = DiscretizeGraphics[Disk[{0.5, 0}]];
Subscript[ℛ, 2] = MeshRegion[{{0, 0}, {1, 0}, {0.25, 0.5}, {0, 1}}, Polygon[{1, 2, 3, 4}]];Show[Subscript[ℛ, 1], Subscript[ℛ, 2]]RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = DiscretizeGraphics[Ball[]];
Subscript[ℛ, 2] = DiscretizeGraphics[Cuboid[]];Show[Subscript[ℛ, 2], Subscript[ℛ, 1]]RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Compare BoundaryMeshRegion in
:
Subscript[ℛ, 1] = BoundaryMeshRegion[{{-1}, {2}}, Point[{1, 2}]];
Subscript[ℛ, 2] = BoundaryMeshRegion[{{0}, {1}}, Point[{1, 2}]];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = BoundaryDiscretizeGraphics[Disk[{0.5, 0}]];
Subscript[ℛ, 2] = BoundaryMeshRegion[{{0, 0}, {1, 0}, {0.25, 0.5}, {0, 1}}, Line[{1, 2, 3, 4, 1}]];Show[Subscript[ℛ, 1], Subscript[ℛ, 2]]RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = BoundaryDiscretizeGraphics[Ball[]];
Subscript[ℛ, 2] = BoundaryDiscretizeGraphics[Cuboid[]];Show[Subscript[ℛ, 2], Subscript[ℛ, 1]]RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Compare MeshRegion with BoundaryMeshRegion in
:
Subscript[ℛ, 1] = ConvexHullMesh[RandomReal[{-2, 2}, {25, 2}]];
Subscript[ℛ, 2] = DelaunayMesh[RandomReal[{-1, 1}, {25, 2}]];Show[Subscript[ℛ, 1], Subscript[ℛ, 2]]RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Subscript[ℛ, 1] = ConvexHullMesh[RandomReal[{0, 1}, {25, 3}]];
Subscript[ℛ, 2] = DelaunayMesh[RandomReal[{-1, 1}, {25, 3}]];Show[Subscript[ℛ, 2], Subscript[ℛ, 1]]RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Derived Regions (1)
Compare BooleanRegion:
Subscript[ℛ, 1] = BooleanRegion[Xnor, {Disk[], Disk[{1, 0}]}];
Subscript[ℛ, 2] = Disk[{1 / 2, 0}, 1 / 4];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Options (2)
Assumptions (1)
Find all disks that contain the unit circle:
Subscript[ℛ, 1] = ImplicitRegion[x^2 + y^2 ≤ r^2, {x, y}];
Subscript[ℛ, 2] = Circle[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2], Assumptions -> r > 0]GenerateConditions (1)
Find when the unit disk lies within an implicitly described annulus:
Subscript[ℛ, 1] = ImplicitRegion[Subscript[r, 1] ≤ x^2 + y^2 ≤ Subscript[r, 2], {x, y}];
Subscript[ℛ, 2] = Disk[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Show the conditions for which the result is valid:
RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2], GenerateConditions -> True]Explicitly allow for degenerate cases:
RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2], Assumptions -> Subscript[r, 1]∈Reals]Applications (7)
All convex combinations of points lie within their convex hull:
ℛ = ConvexHullMesh[RandomReal[1, {25, 2}]];convexcombos = Line[Subsets[MeshCoordinates[ℛ], {2}]];RegionWithin[ℛ, convexcombos]Show[ℛ, Epilog -> convexcombos]Find the largest disk contained in a given triangle:
ℛ = Triangle[{{0, 0}, {4, 0}, {3, 1}}];disk = Disk[{x, y}, r];Maximize[{r, RegionWithin[ℛ, disk]}, {x, y, r}]//Last//SimplifyN[%]Graphics[{ℛ, Red, disk /. %}]Approximate the largest axes-aligned ellipse contained in the triangle:
ellipse = Disk[{x, y}, {a, b}];NMaximize[{Area[ellipse], RegionWithin[ℛ, ellipse]}, {{x, 0, 4}, y, a, b}]Graphics[{ℛ, Red, ellipse /. Last[%]}]Find the smallest disk that contains a given triangle:
ℛ = Triangle[{{0, 0}, {4, 0}, {3, 1}}];disk = Disk[{x, y}, r];Minimize[{r, RegionWithin[disk, ℛ]}, {x, y, r}]//LastGraphics[{disk /. %, Red, ℛ}]Find the smallest axes-aligned ellipse that contains the triangle:
ellipse = Disk[{x, y}, {a, b}];Minimize[{Area[ellipse], RegionWithin[ellipse, ℛ]}, {x, y, a, b}]//FullSimplifyN[%]Graphics[{ellipse /. Last[%], Red, ℛ}]Find all countries that lie completely within the Caribbean Sea:
c = Entity["Ocean", "CaribbeanSea"];
sea = c["Polygon"] /. GeoPosition -> Identity;all = EntityValue["Country", "Polygon", "EntityAssociation"] /. GeoPosition -> Identity;Select the countries whose polygons lie within the Caribbean Sea:
Keys[Select[all, RegionWithin[sea, #]&]]//SortGeoEntities[c, "Country", "FullyContained" -> True]//SortView these countries on a map:
GeoGraphics[{EdgeForm[Black], Polygon[c], GeoStyling[Red], Polygon[%]}]Find and visualize all positions where a unit rectangle lies within an annulus:
Subscript[ℛ, 1] = Annulus[{0, 0}, {1, 3}];
Subscript[ℛ, 2] = Rectangle[{x, y}];cond = RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]];RegionPlot[cond, {x, -3, 3}, {y, -3, 3}, Epilog -> {Red, Opacity[0.1], Subscript[ℛ, 1]}]Perform a random walk inside a region:
ℛ = Annulus[];Define a function to walk a point in a random direction, staying inside a region:
walkPoint[pt_, r_, reg_] :=
Block[{npt = RandomPoint[Sphere[pt, r]]},
While[!RegionWithin[reg, Line[{pt, npt}]],
npt = RandomPoint[Sphere[pt, r]]
];
npt
]Simulate a random walk from an initial point:
start = {0.5, 0.5};path = NestList[walkPoint[#, 0.1, ℛ]&, start, 500];Graphics[{{Green, ℛ}, {Line[path]}, {Red, PointSize[Large], Point[start]}}]Use RegionWithin as a partial ordering to visualize concentric disks:
regs = Disk[{0, 0}, #]& /@ RandomReal[{1 / 4, 1}, 6];colors = RandomColor[Hue[_], 6];Disks with larger radii can completely cover other disks:
Graphics[{EdgeForm[Black], Transpose[{colors, regs}]}]Sort the regions according to membership:
order = Ordering[regs, All, RegionWithin];Visualize the rearranged disks:
Graphics[{EdgeForm[Black], Transpose[{colors, regs}][[order]]}]Properties & Relations (5)
Use RegionMember to test the membership of a point:
ℛ = Parallelogram[];RegionMember[ℛ, {1, 1 / 2}]Use RegionWithin to test the membership of a region:
RegionWithin[ℛ, Point[{1, 1 / 2}]]RegionWithin[ℛ, Disk[{1, 1 / 2}, 1 / 3]]When ℛ2 is within ℛ1, all points in ℛ2 are members of ℛ1:
Subscript[ℛ, 1] = Rectangle[{-1, -1}, {1, 1}];
Subscript[ℛ, 2] = Disk[];Resolve[ForAll[{x, y}, {x, y}∈Subscript[ℛ, 2], {x, y}∈Subscript[ℛ, 1]]]RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]Check if two regions are equal:
Subscript[ℛ, 1] = Parallelogram[{0, 0}, {{1, 0}, {0, 1}}];
Subscript[ℛ, 2] = Rectangle[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]] && RegionWithin[Subscript[ℛ, 2], Subscript[ℛ, 1]]RegionEqual[Subscript[ℛ, 1], Subscript[ℛ, 2]]For non‐empty regions, RegionDisjoint returns False when RegionWithin returns True:
Subscript[ℛ, 1] = Disk[];
Subscript[ℛ, 2] = Triangle[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]RegionDisjoint[Subscript[ℛ, 1], Subscript[ℛ, 2]]Use FindInstance to find points that violate the subset condition:
Subscript[ℛ, 1] = Parallelogram[];
Subscript[ℛ, 2] = Rectangle[];RegionWithin[Subscript[ℛ, 1], Subscript[ℛ, 2]]pts = {x, y} /. FindInstance[{x, y}∈Subscript[ℛ, 1]∧{x, y}∉Subscript[ℛ, 2], {x, y}, 100];Graphics[{Point[pts], Opacity[0.5], {Red, Subscript[ℛ, 2]}, {Green, Subscript[ℛ, 1]}}]Use RandomPoint to find a uniform sampling of points that violate the subset condition:
pts = RandomPoint[BooleanRegion[#1∧!#2&, {Subscript[ℛ, 1], Subscript[ℛ, 2]}], 100];Graphics[{Point[pts], Opacity[0.5], {Red, Subscript[ℛ, 2]}, {Green, Subscript[ℛ, 1]}}]Use Reduce to find where the subset condition is violated:
Reduce[{x, y}∈Subscript[ℛ, 2]∧{x, y}∉Subscript[ℛ, 1], {x, y}]RegionPlot[%, {x, -1, 3}, {y, -3 / 2, 5 / 2}, PlotPoints -> 60]Related Guides
History
Text
Wolfram Research (2017), RegionWithin, Wolfram Language function, https://reference.wolfram.com/language/ref/RegionWithin.html.
CMS
Wolfram Language. 2017. "RegionWithin." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/RegionWithin.html.
APA
Wolfram Language. (2017). RegionWithin. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/RegionWithin.html
BibTeX
@misc{reference.wolfram_2026_regionwithin, author="Wolfram Research", title="{RegionWithin}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/RegionWithin.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_regionwithin, organization={Wolfram Research}, title={RegionWithin}, year={2017}, url={https://reference.wolfram.com/language/ref/RegionWithin.html}, note=[Accessed: 12-June-2026]}