How to create a perimeter from x,y coordinates

Hello everybody,
I have a small question:
I have two matrices: A(2099x2) and B(9x4).
Matrix A = trajectory (x,y)
Matrix B = X,Y position of 9 (black square dots), the width=0.20 and the height= 0.20.
I would like to know how to create a perimeter around of the black square dots (green square) and calculate if the trajectory (blue line) enter in this perimeter. Thanks in advance

Answers (1)

The easiest way, assuming R2017b or later, is to use the intersect method of the polyshape object, which you can construct either by providing the 4 corners of your perimeter or with nsidedpoly:
perimeterside = 0.2;
for blackdotidx = 1:size(B, 1) %iterate over the black dots
perimeter = nsidedpoly(4, 'Center', A(blackdotidx, :), 'SideLength', perimeterside);
if ~isempty(intersect(perimeter, A))
%1st output of intersect is segments inside the perimeter, so if not empty there is intersection
fprintf('trajectory intersect perimeter %d\n', blackdotidx);
end
end

5 Comments

Thank you for your answer. I don't have the earlier version of Matlab. however, I'm working in a code but It does not work:
reach_target= zeros(9,1);
for i = 1:length(X)
if ((x > X(i)) - (w/2)) & ((x < X(i)) + (w/2))
if ((y > Y(i)) - (h/2)) & ((y < Y(i)) + (h/2))
reach_target(i)=1;
end
end
end
"I don't have the earlier version of Matlab" Do you mean you're not on R2017b or R2018a or that you are?
If you are, then you can use the code that I wrote. I don't see what your code has anything to do with it.
Thanks for your answer. I'm working with an online version of Matlab. It does not allow me to use this functions. Yes, I have some difficulties with my code. I would like to know if the trajectory (blue line) passes through the green area (green square) around the black square. the idea is to know if the trajectory approches the positions.
Matlab online is always on the last version. The code I wrote does not require any particular toolbox so is guaranteed to work with matlab online.
I understood very well what you want to do, my answer does just that.
Thanks again Guillaume, I will use your code. How can I store the results in a vector?

Sign in to comment.

Categories

Asked:

on 20 Mar 2018

Commented:

on 22 Mar 2018

Community Treasure Hunt

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

Start Hunting!