Finding index between two parallel lines

Hello,
I have bathymetry data (an intuition is attached here) which is stored in a regular 10m-resolution grid with the following limits:
  • In the x-axis: xmin:10:xmax. This contains 3039 columns
  • In the y-axis: ymin:10:ymax. This forms 1195 rows
  • The depth is Z matrix whose size is equal to 1195x 3039;
And I have 2 parallel lines crossing this bathymetry with the following information:
  • Line 1 is defined as points (x1,y1) and (x2,y2)
  • Line 2 is defined as points (x3,y3) and (x4,y4)
My task is to find indices between these two lines. Could you please help to solve this?
Many thanks
T

2 Comments

My task is to find indices between these two lines.
‘Index’ means different things in different contexts (e.g. matrix, book, etc.). How do you define it here?
wave_buoys
wave_buoys on 10 Mar 2019
Edited: wave_buoys on 10 Mar 2019
Hi, Star Strider,
I have updated the intuition as an photo attached. The indices are defined based on the matrix Z (water depth) which is bounded by xmin, xmax, ymin, ymax.
The area to find indices is a parallelogram, so it might be more tricky than a square or rectangular shape. In other words, I can't use idx=find (x>= xmin & x<= xmax & y>=ymin & x<=ymax) to define the indices in the parallelogram region.
Thanks

Sign in to comment.

 Accepted Answer

You can use inpolygon
clc, clear
xmin = 50;
xmax = 551;
ymin = -35;
ymax = -800;
y1 = ymax + 150;
y4 = ymin - 150;
k_up = (y1 - ymin)/(xmax-xmin);
k_down = (ymax - y4)/(xmax-xmin);
cla, hold on
for x = xmin:10:xmax
y_start = ceil(y1 - x*k_up);
y_end = floor(ymax - x*k_down);
for y = y_start:-10:y_end
plot(x,y,'.r')
end
end
hold off

1 Comment

wave_buoys
wave_buoys on 10 Mar 2019
Edited: wave_buoys on 10 Mar 2019
Hi darova ,
Thanks for the code.
it looks you are on the right track. Using inpolygon works for me! Here is my approach:
polx=[2908,2943,2938,2916]; % define x-coordinates for the polygon
poly=[5757,5760,5759,5756]; % define y-coordinates for the polygon
% XB, YB: two-dimensional matrices defined by meshgrid(xmin:10:xmax,ymin:10:ymax);
[in,out]=inpolygon(XB,YB,polx,poly); % this returns logical values
nums_idx = double(in); % then I converted into numbers
idxall=find(nums_idx); % find non-zero values
XC=XB(idxall); YC=YB(idxall); % this returns to indices inside the polygon!
Thanks for your help with suggesting to use inpolygon again.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018a

Asked:

on 10 Mar 2019

Edited:

on 10 Mar 2019

Community Treasure Hunt

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

Start Hunting!