MATLAB: How to use if statement to know if a point is inside or outside of a cell

blong tocell

I want to use 'if statement' as such that if the points are inside the cell (say C{7}), it will display 'inside cell' and if it is outside the cell (say C{7}) it displays 'outside cell'.
I wrote a piece of code to sort of illustrate what I want to do but it does not seem to be working.
clc,clear all
x_bis = [2 5 8 8 5 2 5];
y_bis = [1 1 1 9 9 9 5];
[V,C] = voronoin([x_bis' y_bis'])
voronoi(x_bis,y_bis)
axis([0 10 0 10])
hold on
P = [5 5];
plot(P(:,1),P(:,2),'*k')
if P == C(7) %in other words, if the point is inside the cell C{7}
display('inside cell')
else
display('outside cell')
end

Best Answer

You need inpolygon function to check if a point is inside a polygon.
clc,clear all
x_bis = [2 5 8 8 5 2 5];
y_bis = [1 1 1 9 9 9 5];
[V,C] = voronoin([x_bis' y_bis']);
voronoi(x_bis,y_bis)
axis([0 10 0 10])
hold on
P = [5 5];
plot(P(:,1),P(:,2),'*k')
Polygon_C7 = V(C{7},:);
if inpolygon(P(1), P(2), Polygon_C7(:,1), Polygon_C7(:,2))
disp('inside cell')
else
disp('outside cell')
end