determine if one shape is fully enclosed inside another shape

Is there a function similar to overlaps for polyshape objects, that checks if polyshape A is fully contained inside polyshape B? for example I would want to replace the function 'overlaps' with something so the following code returns: TF21=1 (because 2 is fully contained in 1), TF31=0 (because 3 is not fully contained in 1), TF23=0 (because 2 is not fully contained in 3), and TF12=0 (because 1 is not fully contained in 2). A function that determines the same true or fale just using the vectors would work as well, but i dont want to use inpolygon function because it takes a long time
p1 = polyshape([0 0 1 1],[1 0 0 1]);
p2 = polyshape([.2 .6 .6 .2],[.2 .2 .6 .6]);
p3 = polyshape([0.75 1.25 1.25 0.75],[0.25 0.25 0.75 0.75]);
plot([p1 p2 p3]);
TF21=overlaps(p2,p1)
TF31=overlaps(p3,p1)
TF23=overlaps(p2,p3)
TF12=overlaps(p1,p2)

Answers (2)

For p and q two polyshapes:
pmq=subtract(p,q);
qmp=subtract(q,p);
include = pmq.NumRegions==0 || qmp.NumRegions==0

2 Comments

This returns true when testing if p2 is within p1 and it returns true when testing if p1 is within p2.
Yes that is exactly my intention.

Sign in to comment.

Here's a function that returns a logical true/false to determine if the polyshape p1 is fully within p2 by comparing the area of p1 to the area of the void created by p2-p1.
% Annonymous function that tests whether polyshape object ps1 is fully within
% polyshape object ps2. TF = isInside(ps1,ps2)
isInside = @(ps1,ps2)abs(area(ps2)-area(subtract(ps2,ps1))-area(ps1))<(area(ps1)*1e-06);
Here's a test where we expect a TRUE result
t = 0.05:0.5:2*pi;
x1 = cos(t) + 1.02;
y1 = sin(t) + 1.02;
p1 = polyshape({x1},{y1});
p2 = polyshape([0 0 1 3], [0 3 3 0]);
plot([p1,p2])
TF = isInside(p1,p2) %TF = 1
Here's a test where we expect a FALSE result (the upper, right corner of p1 protrudes)
t = 0.05:0.5:2*pi;
x1 = cos(t) + 1.1;
y1 = sin(t) + 1.1;
p1 = polyshape({x1},{y1});
p2 = polyshape([0 0 1 3], [0 3 3 0]);
plot([p1,p2])
TF = isInside(p1,p2) %TF = 0
Methods: The function computes the area of p1, p2, and p2 minus any overlap of p1 (we'll call this shape p2s). If p1 is completely within p2, then the area of p2s minus the area of p2 minus the area of p1 should be zero. Due to round-off error, the sum might not exactly be 0 so we accept any sum that is less than one millionth of the area of P1 (area(p1) * 1/1000000).

Categories

Products

Release

R2018b

Asked:

on 26 Jul 2019

Edited:

on 29 Jul 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!