1

I'm searching for some efficient matlab code that checks if a line segment intersects a square. The square is axis-aligned, the line segment does not have to be axis aligned. It is a decision procedure, i.e. return Y or N, so I do not require intersection point(s). Each line segment has a start point, and an end point.

I'm starting with a 2-D case, but will require code that works for d-dimensional space.

I have fast code for the simple cases:

1) line segment enters a square (start point outside of square, end point inside square).

2) line segment exits a square (start point inside square, end point outside square).

3) line segment within a square (start and end point inside square).

4) line segment is far and does not intersect square (start and end point outside square and bounding box of line segment does not cover any part of square).

However, I do not have any code yet for the less simple check where the line segment start and end points are outside the square, and the line segment bounding box covers part (or all) of the square.

In this case, the line segment could: i) intersect the tip of a square corner, ii) intersect two square edges, or iii) not intersect the square at all.

Any ideas on how to test this last case? And how to make the code work for d-dimensional line segments and squares (cubes, etc)?

Thanks!

To help visualize this, I am updating this post with some matlab code that tests cases 1-4 above.

 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?
  • Welcome to SO!... For any suggestions as source code you should specify the data that describes a line and a square. A line must be described by at least two points in space, which can be represented as a 2 x n array or cell or a structure with two fields. An n-dimensional square can be represented by n intervals, which again has several opportunities to be setup in MATLAB. – Sven-Eric Krüger Mar 13 '19 at 06:05
  • Please provide your code, so that people can work on your ideas. – HansHirse Mar 13 '19 at 07:08
  • For the 2D case, matlab include a new built-in `intersect` function (r2017b). Please provide your code and show what you've already done. – obchardon Mar 13 '19 at 09:19
  • The line segment intersects the rectangle of it intersects any of its sides. Search SO for the algorithm to compute the intersection between two segments, I’ve seen it here before. The sides being axis-aligned will make the code quite a bit simpler. – Cris Luengo Mar 14 '19 at 03:04
  • Thanks for the comment @cris. I'm hoping to avoid the computations relating to testing the intersection of a line segment and box edges. For a 2-d box there are only 4 edges, but for a 3-d box there are 12 edges, and for a 10-d box there are 5120 edges. The number of edges is exponential w.r.t the number of dimensions. I'm hoping there is some math trick to compute it another way, or at least reduce the number of intersection computations. – user11162708 Mar 14 '19 at 05:52
  • For a 3d box there are 6 sides, and for a 10d box there are 20 "sides". There are always 2 per dimension. I think that if you implement this you'll find it's easier than you're thinking, and will make some of your current partial tests redundant (i.e. they won't make your code faster or shorter). – Cris Luengo Mar 14 '19 at 05:58

0 Answers0