MATLAB Answers

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

38 views (last 30 days)
Ashley
Ashley on 25 Apr 2014
Commented: Albert on 5 Oct 2017
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

Answers (1)

Star Strider
Star Strider on 25 Apr 2014
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 Comments
Albert
Albert on 5 Oct 2017
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!