1

我正在寻找一些有效的 matlab 代码来检查线段是否与正方形相交。正方形是轴对齐的,线段不必是轴对齐的。这是一个决策过程,即返回 Y 或 N,所以我不需要交点。每条线段都有一个起点和一个终点。

我从二维案例开始,但需要适用于 d 维空间的代码。

我有简单案例的快速代码:

1)线段进入一个正方形(起点在正方形外,终点在正方形内)。

2)线段出正方形(起点在正方形内,终点在正方形外)。

3)正方形内的线段(正方形内的起点和终点)。

4)线段较远且不与正方形相交(起点和终点在正方形之外,线段的边界框不覆盖正方形的任何部分)。

但是,我还没有任何代码用于不太简单的检查线段起点和终点在正方形之外的位置,以及线段边界框覆盖正方形的一部分(或全部)。

在这种情况下,线段可以:i) 与方形角的尖端相交,ii) 与两个方形边相交,或 iii) 根本不与方形相交。

关于如何测试最后一个案例的任何想法?以及如何使代码适用于 d 维线段和正方形(立方体等)?

谢谢!

为了帮助可视化这一点,我用一些测试上述案例 1-4 的 matlab 代码更新了这篇文章。

 s = [1 4]; % line segment start point, [x y] coordinates
 e = [3 2]; % line segment end point, [x y] coordinates

 % define the square. 
 % instead of defining the four corners, describe the square as a bounding box.
 % first row is set of min coordinates, second row is set of max coordinates.
 % first column is x coordinate, second column is y coordinate.
 sq = [2 1;6 5];   

 % now, the code below tests if the line segment start and end points are covered by the square.
 % note that this code works for d-dimensional space for cases 1-5 below.

 % for each line segment start point coordinate, test if it is >= both the min and max square coordinates
 tmpS = s >= sq; 

 % if the line segment start point coordinates are all >= the square min coordinates, 
 % and the line segment start point coordinates are all < the square max coordinates, 
 % then the line segment start point is covered by the square.

 coveredS = all(tmpS(1,:) == 1) && all(tmpS(2,:) == 0);

 % now simply do the same for the end point line segment
 tmpE = e >= sq; 
 coveredE = all(tmpE(1,:) == 1) && all(tmpE(2,:) == 0);

 % now there is enough information to test if we are in cases 1,2, and 3.
 if coveredS == false && coveredE == true
     disp('Case 1: line segment enters a square');
 elseif coveredS == true && coveredE == false
     disp('Case 2: line segment exits a square');
 elseif coveredS == true && coveredE == true
     disp('Case 3: line segment within a square');
 else
     disp('Case still unknown! Both start and end points are outside of square');
 end

 % for case 4 create a bounding box around the line segement and
 % compare it to the square
 bb = [min([s; e]); max([s; e])]; % line segment bounding box

 % if any of the line seg bounding box min coordinates are > the square max
 % coordinates, or any of the line seg bounding box max coordinates are <
 % the square min coordinates, then the line seg bb does not cover any part
 % of the square.
 if any(bb(1,:) > sq(2,:) == 1) || any(bb(2,:) < sq(1,:) == 1)
     disp('Case 4: line segment is far and does not intersect square');
 end

 % now, if all the above checks fail, then the line seg bounding box
 % partially (or fully) covers the square, and the line seg start and end 
 % points are not covered by the square.  But, we do not know if the line 
 % segment in this state intersects the square or not.  An additional check 
 % must be made here.

 % so this is the question of my post - what is the the most efficient code
 % that can check this state, and also works for d-dimensional space?
4

0 回答 0