Alex
Alex

Reputation: 25

MATLAB moving a point in the XY plane

In MATLAB, I have an XY plane that is represented by finite number of points. The possible values of x are stored in a vector X and the possible values of y are stored in another vector Y. I have a point, say A, such that A(1) belongs to X is the x-coordinate and A(2) belongs to Y is the y-coordinate.

This point A can move in one of 8 ways if it is in the middle:

          .   .   .                      .   A                .    .

          .   A   .        OR            .   .       OR       .    A

          .   .   .                                           .     .

Of course, the set of these points changes if the point A is on the edge (sometimes only 5, sometimes only 3 if it is a corner). How can I find the set of these "1-hop" neighboring points? What about the set of "k-hop" neighboring points? By set I mean two vectors one for x-coordinates and another for y-coordinates. Thanks!

Upvotes: 0

Views: 1289

Answers (2)

Amro
Amro

Reputation: 124563

Consider the following code:

%# create grid of 2D coordinates
sz = [5 6];
[X,Y] = meshgrid(1:sz(2),1:sz(1));

%# point A
A = [1 2]

%# neighboring points
k = 2;                               %# hop size
[sx,sy] = meshgrid(-k:k,-k:k);       %# steps to get to neighbors
xx = bsxfun(@plus, A(1), sx(:));     %# add shift in x-coords
xx = min(max(xx,1),sz(2));           %# clamp x-coordinates within range
yy = bsxfun(@plus, A(2), sy(:));
yy = min(max(yy,1),sz(1));
B = unique([xx yy],'rows');          %# remove duplicates
B(ismember(B,A,'rows'),:) = [];      %# remove point itself

The result for the point A = (1,2) with k=2 hops:

B =
     1     1
     1     3
     1     4
     2     1
     2     2
     2     3
     2     4
     3     1
     3     2
     3     3
     3     4

and an illustration of the solution:

x A x x . .
x x x x . .
x x x x . .
. . . . . .
. . . . . .

Upvotes: 1

magarwal
magarwal

Reputation: 574

lets say A = [Xcenter Ycenter]

for K-hop, you can access points:

pointsX = [];
pointsY = [];
for i=-k:k
  pointsX = [pointsX  Xcenter+i]; 
  pointsY = [pointsY  Ycenter+i];
end

Furthermore, you can filter these points by order coordinates and remove the outliers. e.g. consider

(1,1)  (1,2)  (1,3)
(2,1)  (2,2)  (2,3)
(3,1)  (3,2)  (3,3)

Now you know that minimum allowed X and Y are 1, so just filter out points with any ordinate and/or abscissa lesser than that.

Upvotes: 0

Related Questions