How to determine which grid cells a line segment passes through?

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.

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.

时间: 2024-10-18 18:04:21

How to determine which grid cells a line segment passes through?的相关文章

jqwidgets: Grid Cells Formatting

http://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxgrid/jquery-grid-cellsformatting.htm $("#jqxgrid").jqxGrid( { width: 670, height: 450, source: source, theme: theme, sortable: true, columns: [ { text: 'Ship Name', datafield

[Selenium+Java] Selenium Grid Tutorial: Command Line and JSON Example

Original URL: https://www.guru99.com/introduction-to-selenium-grid.html What is Selenium Grid? Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in para

目标检测之线段检测---lsd line segment detector

(1)线段检测应用背景 (2)线段检测简介 (3)线段检测实例 a line segment detector (4)hough 变换和 lsd 的区别 ---------------------author:pkf ------------------------------time:2015-1-26 -----------------------------------------qq:1327706646 (1)线段检测应用背景 线段检测在高铁电机机车顶部图像检测系统中有很大应用,像受电

Android OpenGL ES(九)绘制线段Line Segment .

创建一个DrawLine Activity,定义四个顶点: float vertexArray[] = { -0.8f, -0.4f * 1.732f, 0.0f, -0.4f, 0.4f * 1.732f, 0.0f, 0.0f, -0.4f * 1.732f, 0.0f, 0.4f, 0.4f * 1.732f, 0.0f, }; 分别以三种模式GL_LINES,GL_LINE_STRIP,GL_LINE_LOOP 来绘制直线: public void DrawScene(GL10 gl)

UVA 11355 Cool Points

Cool Points We have a circle of radius R and several line segments situated within the circumference of this circle. Let's define a cool point to be a point on the circumference of this circle so that the line segment that is formed by this point and

UVA 11355 Cool Points( 极角计算 )

We have a circle of radius R and several line segments situated within the circumference of this circle. Let’s define a cool point to be a point on the circumference of this circle so that the line segment that is formed by this point and the centre

N-Dimensional Grid

You are given an n-dimensional grid in which the dimensions of the grid are a1?×?a2?×?...?×?an. Each cell in the grid is represented as an n-tuple (x1,?x2,?...,?xn) (1?≤?xi?≤?ai). Two cells are considered to be adjacent if the Manhattan Distance betw

Codeforces Round #589 (Div. 2) B——B. Filling the Grid

Suppose there is a h×wh×w grid consisting of empty or full cells. Let's make some definitions: riri is the number of consecutive full cells connected to the left side in the ii-th row (1≤i≤h1≤i≤h). In particular, ri=0ri=0 if the leftmost cell of the 

hust 1013 Grid

题目描述 There is a grid size of 1*N. The spanning tree of the grid connects all the vertices of the grid only with the edges of the grid, and every vertex has only one path to any other vertex. Your task is to find out how many different spanning trees