【问题标题】:How to get line rectangle intersection segment?如何获得直线矩形相交段?
【发布时间】:2014-09-25 07:05:00
【问题描述】:

我想找到代数重建方法的权重矩阵。为此,我必须找到与网格的线交点。我可以找到直线与直线的直接交点,但我必须明智地存储相交的线段网格数。所以假设如果在网格中第一个正方形不与网格相交,那么将零放在权重矩阵的第一个元素上。

这里是我尝试的线交点代码:

ak = 3:6
aka = 3:6
x = zeros(size(aka))
y = zeros(size(ak))
for k = 1:length(ak)
  line([ak(1) ak(end)], [aka(k) aka(k)],'color','r')
end

% Vertical grid
for k = 1:length(aka)
  line([ak(k) ak(k)], [aka(1) aka(end)],'color','r')
end
hold on;
 X =[0 15.5]
 Y = [2.5 8.5] 
 m = (Y(2)-Y(1))/(X(2)-X(1)) ;
 c = 2.5 ; 
 plot(X,Y)
axis([0 10 0 10])
axis square
% plotting y intercept
for i = 1:4
    y(i) = m * ak(i) + c
    if y(i)<2 || y(i)>6
        y(i) = 0
    end
end
% plotting x intercept
for i = 1:4
   x(i) = (y(i) - c)/m 
    if x(i)<2 || x(i)>6
        x(i) = 0
    end
end  
z = [x' y']

我有一条线,由参数m, h 定义,其中y = m*x + h 这条线穿过网格(即像素)。

对于网格的每个正方形(a, b)(即正方形[a, a+1]x[b, b+1]),我想确定给定的线是否穿过这个正方形,如果是,那么正方形中线段的长度,这样我就可以构造代数重建方法所必需的权重矩阵。

【问题讨论】:

  • 我不知道您需要什么帮助。您的代码正在绘制一条线和一个网格,网格大小为 1 从 3 到 6。并返回 3、4、5 和 6 处的 y 值。您想要什么作为最终结果,确切的问题是什么?
  • @TheMinion 我有一条线,由参数 m, h 定义,其中 y = m*x + h 这条线穿过网格(即像素)。对于网格的每个正方形 (a, b)(即正方形 [a, a+1] x [b, b+1]),我想确定给定的线是否穿过这个正方形,如果是,正方形中线段的长度是多少。这样我就可以构造代数重建方法所必需的权重矩阵。
  • @ParthPatel 考虑将问题的标题更改为“如何在 matlab 中获取直线矩形相交段”。我在下面回答了你的问题,并举例说明了如何做到这一点。
  • @DontCareBear 谢谢 :) 我做了不同的方法 :) 但你的方法似乎很有效:)
  • 在 matlab 中找到解决这些问题的方法总是很好的。您可以将我给您的代码用于任何四边形或三角形网格。玩得开心:)

标签: matlab line-intersection tomography-reconstruction


【解决方案1】:

这是一个很好的方法,可以将一条线与矩形网格相交并获取每个相交段的长度:我在此 link 的第三个答案中使用伪代码中的线线相交

% create some line form the equation y=mx+h
m = 0.5; h = 0.2;
x = -2:0.01:2;
y = m*x+h;
% create a grid on the range [-1,1]
[X,Y] = meshgrid(linspace(-1,1,10),linspace(-1,1,10));
% create a quad mesh on this range
fvc = surf2patch(X,Y,zeros(size(X)));
% extract topology
v = fvc.vertices(:,[1,2]);
f = fvc.faces;
% plot the grid and the line
patch(fvc,'EdgeColor','g','FaceColor','w'); hold on;
plot(x,y);
% use line line intersection from the link
DC = [f(:,[1,2]);f(:,[2,3]);f(:,[3,4]);f(:,[4,1])];
D = v(DC(:,1),:);
C = v(DC(:,2),:);
A = repmat([x(1),y(1)],size(DC,1),1);
B = repmat([x(end),y(end)],size(DC,1),1);
E = A-B;
F = D-C;
P = [-E(:,2),E(:,1)];
h = dot(A-C,P,2)./dot(F,P,2);
% calc intersections
idx = (0<=h & h<=1);
intersections = C(idx,:)+F(idx,:).*repmat(h(idx),1,2);
intersections = uniquetol(intersections,1e-8,'ByRows',true);
% sort by x axis values
[~,ii] = sort(intersections(:,1));
intersections = intersections(ii,:);
scatter(intersections(:,1),intersections(:,2));
% get segments lengths
directions = diff(intersections);
lengths = sqrt(sum(directions.^2,2));
directions = directions./repmat(sqrt(sum(directions.^2,2)),1,2);
directions = directions.*repmat(lengths,1,2);
quiver(intersections(1:end-1,1),intersections(1:end-1,2),directions(:,1),directions(:,2),'AutoScale','off','Color','k');

这是结果(图中箭头的长度为段长)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2020-12-17
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多