MATLAB moving a point in the XY plane

后端 未结 2 1541
傲寒
傲寒 2020-12-22 05:42

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 an

相关标签:
2条回答
  • 2020-12-22 06:20

    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 . .
    . . . . . .
    . . . . . .
    
    0 讨论(0)
  • 2020-12-22 06:39

    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.

    0 讨论(0)
提交回复
热议问题