Why is vec2mtx missing some grid points?

4 次查看(过去 30 天)
I am using vec2mtx to figure out which cells in a regular data grid are intersected by a vector of latitude-longitude coordinates.
However, I noticed that vec2mtx is missing some of these cells.
Here is a minimum working example:
lats=[1.5,2.5]; lons=[1.5,3.25]; % just a straight line
[Z,R]=vec2mtx(lats,lons,1,[0,4],[0,4]); % convert those coordinates to a grid
figure; grid2image(Z,R); % plot the grid
hold on; plot(lons,lats,'r'); hold off; % plot the straight line
set(gca,'XTick',0:2,'YTick',0:3,'GridColor','k','GridAlpha',1); % just some adjustments to make the grid clearer
grid on;
This code generates the following figure:
where the yellow cells are equal to 1, and the blue equal to 0.
I was expecting the cell marked with the data cursor to be yellow too, considering that the border (red line) is crossing that cell too.
Is there any way to make sure vec2mtx not to miss cells like in the example above? Or, is there an alternative function to achieve what I am trying here?

回答(1 个)

George Abrahams
George Abrahams 2024-2-10
vec2mtx is an implementation of a line drawing algorithm. It takes a continuous line segment and converts it to a discrete grid of pixels.
There are many different algorithms, and I'm not sure which vec2mtx is using. However, the naïve approach you described, filling every pixel the line touches, is typically avoided as it creates lines which are very thick. See the figure below (source) for a comparison.
If you'd like to learn more, here's a good resource too:

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by