0

Lets say I have a line defined in 3 dimensions like so:

class Line{
    float startx;
    float starty;
    float startz;
    float endx;
    float endy;
    float endz;
};

as well as a rectangle defined in 3 dimensions:

class Rectangle{
    float ax;
    float ay;
    float az;
    float bx;
    //...
    float dz;
};

How would I make a function to determine at what point, if at all, a given line and rectangle intersect?

Kai Schmidt
  • 701
  • 8
  • 14
  • Are you having trouble with the Math or the turning it into code part? – chris Jul 23 '15 at 04:04
  • I suppose I could work it out on paper, but how do I do it as code? I started writing it down as all variables, but it quickly got very complicated. I was hoping there might be an easier way to do it in code, possibly using matrices. – Kai Schmidt Jul 23 '15 at 04:06
  • There is already a good answer on testing for the intersection between two lines at [http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect](http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect). If you are only concerned with testing for the intersection between the line and rectangle boundary, then you can apply the line intersect test against each of the four rectangle sides. – VirtualMichael Jul 23 '15 at 04:07
  • Exact one, http://stackoverflow.com/questions/1585525/how-to-find-the-intersection-point-between-a-line-and-a-rectangle – Sachin Nambiar Nalavattanon Jul 23 '15 at 04:09
  • Forgive me if I was not clear enough, but I am concerned with the intersection of the line and the area inside the rectangle, not its edges. Both the line and rectangle exist in 3 dimensions. – Kai Schmidt Jul 23 '15 at 04:10
  • @VirtualMichael By checking only sides, the intersection of line inside the rectangle will be missed. – Satish Chalasani Jul 23 '15 at 04:10
  • The duplicated suggested is only 2D! – Satish Chalasani Jul 23 '15 at 04:19
  • The intersection between a 3D line and a 3D box is most likely a line segment rather than just a point, @KaiSchmidt? – Pang Jul 23 '15 at 05:06
  • It is not a box, it is a rectangle whose points are defined in 3d space, so its intersection with a line whose points are also defined in 3d space would be a 3d point. – Kai Schmidt Jul 24 '15 at 01:26
  • Ah, I misunderstood. Now I see. Maybe this would help? http://stackoverflow.com/questions/8812073/ray-and-square-rectangle-intersection-in-3d – Pang Jul 24 '15 at 02:08

1 Answers1

0

There are multiple ways to do this. One way to do this is :

  • You can divide the rectangle into two triangles.
  • Find the intersection of line with the triangle plane and check if the intersection point is inside or outside by checking its barycentric coordinates. This tell you if intersection is inside or outside. This needs to be done for the second triangle if the intersection is not inside the first one.

Another way is to take advantage of oriented rectangle edges. You can take cross product with the one end of the line with one corner and another cross product with the other point of the line and with the same corner and if they are different directions then you perform more checks. If the the directions are same then no need as it indicates the points of the line are on the same side of the rectangle.