How to determine which grid cells a line segment passes through?
2017-03-09 16:26
141 查看
https://cn.mathworks.com/matlabcentral/answers/230155-how-to-determine-which-grid-cells-a-line-segment-passes-through
How to determine which grid cells a line segment passes through?
Hi, I apologize if this question has been asked before but I have looked and cannot find the answer.Similar to my previous question, lets say you have a grid with cells in the x-direction from [1:5] and in the y-direction from [1:5] (5 x 5 grid). Now lets say you have two points on the grid: (0.35, 2.65) and (4.2,4.73).
Is there a simple way to determine which grid cells the line connecting those two points passes through (and the distances of each line segment)?
It is very easily visually to see which cells the line passes through (see figure below). Clearly the line segment passes through cells B1, C1, C2, D2, D3, E3 and E4.
Determining the distances of each line segment through each cell would be more difficult by hand.
I have currently solved the problem by finding the slope and intercept of the line, then determining the intercepts of the line with the grid cell lines. Using this, I can determine the distance of each line segment. Then, to find which cell the line segment falls in, I have to find the centre point of each grid cell and the midpoint of each line segment and do a for loop to determine the minimum distance between each line segment and each cell centre.
Is there a more elegant way to solve this problem? Obviously when dealing with multiple lines and/or a large grid, this will become much more time intensive.
![](https://cn.mathworks.com/matlabcentral/answers/uploaded_files/34259/Picture1.png)
This brings back memories of How do I store the Coordinates of Lines Intersecting a Grid? I refer you to it for the description and documentation.
I later updated the code for it but didn’t post it. The updated code is:
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 = -5; % Slope (or slope array) b = 15; % 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
hvix = [hrz; vrt]; % Concatanated ‘hrz’ and ‘vrt’ arrays exbd = find( (hvix(:,2) < 0) | (hvix(:,2) > 25) ); hvix(exbd,:) = []; srtd = unique(hvix,'rows'); % Remove repeats and sort ascending by ‘x’
exL1 = find((L1 < 0) | (L1 > 25)); % Find ‘y’ values for ‘L1’ off grid xp = x; % Create plotting x-vector for L1 xp(exL1) = []; % Eliminate out-of-bounds ‘y’ values from ‘x’ L1(exL1) = []; % Eliminate out-of-bounds ‘y’ values from ‘Li’
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(xp, L1) % Plot more lines here (additional ‘plot’ statements) hold off axis equal
It’s been over a year since I wrote it so I would have to study it to remember what I did, but I did my best at the time to document it exhaustively with comments, so that should help. The output (grid intersections) is in the srtd array.
相关文章推荐
- How to determine which version of SQL Server 2000 is running
- How to find the exact line position which causes “undefined is not a function" error
- How to merge grid cells
- How to determine which patches have been applied
- How to pass QEMU command-line options through libvirt
- [quote] How to determine what target is given in Makefile from the make command-line?
- How to determine which version of .net framework are installed
- 【杂】How to determine which edition of Exchange Server is installed
- How to pass QEMU command-line options through libvirt
- How to compare two cells value in Grid via Compare Validator?
- How to determine which checkboxes are checked in postback (sqlstr="delete spprod where id in ("&request("sid")&")")
- How to deploy a .Net assmebly for COM use through CAB on Web Page (转)
- How to check which responsiblity we are using
- 表中真正使用的空间(How to Determine Real Space used by a Table (Below the High Water Mark)
- how to solve the problem which is the imail software can not send email in wan,but can send email in lan.
- How To determine DDIC Check Table, Domain and Get Table Field Text Data For Value?
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
- how to remove 1px line at UINavigationBar
- How to use HttpWebRequest to post data to another page which is on another server