How do I store the Coordinates of Lines Intersecting a Grid?

10 次查看(过去 30 天)
Hello,
I would like to first create a grid that is 5(x-direction) by 25 (y-direction), and each individual box in the grid is basically 1 X 1. Then I want to create lines over this grid, and store the coordinates of where the lines intersect the grid. So, for example, the intersections of where one line intersects the grid would be one matrix, and the next line would be another matrix. Is this even possible? Thank you.
Ashley

回答(1 个)

Star Strider
Star Strider 2014-4-25
This should get you started:
x = 0:5; % X-range
y = 0:25; % Y-range
lxmb = @(x,mb) mb(1).*x + mb(2); % Line equation: y = m*x+b
m = 2; % Slope (or slope array)
b = 1; % Intercept (or intercept array)
mb = [m b]; % Matrix of [slope intercept] values
L1 = lxmb(x,mb); % Calculate Line #1 = y(x,m,b)
hix = @(y,mb) [(y-mb(2))./mb(1); y]; % Calculate horizontal intercepts
vix = @(x,mb) [x; lxmb(x,mb)]; % Calculate vertical intercepts
hrz = hix(x(2:end),mb)' % [X Y] Matrix of horizontal intercepts
vrt = vix(y(1:6),mb)' % [X Y] Matrix of vertical intercepts
figure(1) % Draw grids & plot lines
plot(repmat(x,2,length(x)), [0 length(y)-1]) % Vertical gridlines
hold on
plot([0 length(x)-1], repmat(y,2,length(y))) % Horizontal gridlines
plot(x, L1) % Plot more lines here (additional ‘plot’ statements)
hold off
axis equal
It creates the grid, plots the first line, and gives one matrix of [x y] coöridinates for the horizontal grid intercepts, and another one for the vertical grid intercepts. It is relatively simple to use a loop to generate more lines and grid intercept matrices with a matrix of [m b] values.
Have fun!
  3 个评论
Star Strider
Star Strider 2014-4-25
My pleasure!
It’s an interesting problem, actually.
To get the combined arrays sorted in ascending order and without repeats, add these two lines just after the assignments for hrz and vrt:
hvix = [hrz; vrt]; % Concatanated ‘hrz’ and ‘vrt’ arrays
srtd = unique(hvix,'rows'); % Remove repeats and sort ascending by ‘x’
The srtd array is the one you want.
Albert
Albert 2017-10-5
Hi, Star Strider
There might be a small mistake in your code:
hrz = hix(x(2:end),mb)' % [X Y] Matrix of horizontal intercepts
vrt = vix(y(1:6),mb)' % [X Y] Matrix of vertical intercepts
should it be like:
hrz = hix(y(1:6),mb)'
vrt = vix(x(2:end),mb)'
Thank you

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by