<p>I have two points (a line segment) and a rectangle. I would like to know how to calculate if the line segment intersects the rectangle. </p>

[{'answer_id': 30098, 'author': 'kokos', 'author_id': 1065, 'author_profile': 'https://Stackoverflow.com/users/1065', 'pm_score': 2, 'selected': False, 'text': '<p>Do <a href="http://mathworld.wolfram.com/Line-LineIntersection.html" rel="nofollow noreferrer">http://mathworld.wolfram.com/Line-LineIntersection.html</a> for the line and each side of the rectangle.<br>\nOr: <a href="http://mathworld.wolfram.com/Line-PlaneIntersection.html" rel="nofollow noreferrer">http://mathworld.wolfram.com/Line-PlaneIntersection.html</a></p>\n'}, {'answer_id': 30109, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 5, 'selected': True, 'text': '<p>From my "Geometry" class:</p>\n\n<pre><code>public struct Line\n{\n    public static Line Empty;\n\n    private PointF p1;\n    private PointF p2;\n\n    public Line(PointF p1, PointF p2)\n    {\n        this.p1 = p1;\n        this.p2 = p2;\n    }\n\n    public PointF P1\n    {\n        get { return p1; }\n        set { p1 = value; }\n    }\n\n    public PointF P2\n    {\n        get { return p2; }\n        set { p2 = value; }\n    }\n\n    public float X1\n    {\n        get { return p1.X; }\n        set { p1.X = value; }\n    }\n\n    public float X2\n    {\n        get { return p2.X; }\n        set { p2.X = value; }\n    }\n\n    public float Y1\n    {\n        get { return p1.Y; }\n        set { p1.Y = value; }\n    }\n\n    public float Y2\n    {\n        get { return p2.Y; }\n        set { p2.Y = value; }\n    }\n}\n\npublic struct Polygon: IEnumerable&lt;PointF&gt;\n{\n    private PointF[] points;\n\n    public Polygon(PointF[] points)\n    {\n        this.points = points;\n    }\n\n    public PointF[] Points\n    {\n        get { return points; }\n        set { points = value; }\n    }\n\n    public int Length\n    {\n        get { return points.Length; }\n    }\n\n    public PointF this[int index]\n    {\n        get { return points[index]; }\n        set { points[index] = value; }\n    }\n\n    public static implicit operator PointF[](Polygon polygon)\n    {\n        return polygon.points;\n    }\n\n    public static implicit operator Polygon(PointF[] points)\n    {\n        return new Polygon(points);\n    }\n\n    IEnumerator&lt;PointF&gt; IEnumerable&lt;PointF&gt;.GetEnumerator()\n    {\n        return (IEnumerator&lt;PointF&gt;)points.GetEnumerator();\n    }\n\n    public IEnumerator GetEnumerator()\n    {\n        return points.GetEnumerator();\n    }\n}\n\npublic enum Intersection\n{\n    None,\n    Tangent,\n    Intersection,\n    Containment\n}\n\npublic static class Geometry\n{\n\n    public static Intersection IntersectionOf(Line line, Polygon polygon)\n    {\n        if (polygon.Length == 0)\n        {\n            return Intersection.None;\n        }\n        if (polygon.Length == 1)\n        {\n            return IntersectionOf(polygon[0], line);\n        }\n        bool tangent = false;\n        for (int index = 0; index &lt; polygon.Length; index++)\n        {\n            int index2 = (index + 1)%polygon.Length;\n            Intersection intersection = IntersectionOf(line, new Line(polygon[index], polygon[index2]));\n            if (intersection == Intersection.Intersection)\n            {\n                return intersection;\n            }\n            if (intersection == Intersection.Tangent)\n            {\n                tangent = true;\n            }\n        }\n        return tangent ? Intersection.Tangent : IntersectionOf(line.P1, polygon);\n    }\n\n    public static Intersection IntersectionOf(PointF point, Polygon polygon)\n    {\n        switch (polygon.Length)\n        {\n            case 0:\n                return Intersection.None;\n            case 1:\n                if (polygon[0].X == point.X &amp;&amp; polygon[0].Y == point.Y)\n                {\n                    return Intersection.Tangent;\n                }\n                else\n                {\n                    return Intersection.None;\n                }\n            case 2:\n                return IntersectionOf(point, new Line(polygon[0], polygon[1]));\n        }\n\n        int counter = 0;\n        int i;\n        PointF p1;\n        int n = polygon.Length;\n        p1 = polygon[0];\n        if (point == p1)\n        {\n            return Intersection.Tangent;\n        }\n\n        for (i = 1; i &lt;= n; i++)\n        {\n            PointF p2 = polygon[i % n];\n            if (point == p2)\n            {\n                return Intersection.Tangent;\n            }\n            if (point.Y &gt; Math.Min(p1.Y, p2.Y))\n            {\n                if (point.Y &lt;= Math.Max(p1.Y, p2.Y))\n                {\n                    if (point.X &lt;= Math.Max(p1.X, p2.X))\n                    {\n                        if (p1.Y != p2.Y)\n                        {\n                            double xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;\n                            if (p1.X == p2.X || point.X &lt;= xinters)\n                                counter++;\n                        }\n                    }\n                }\n            }\n            p1 = p2;\n        }\n\n        return (counter % 2 == 1) ? Intersection.Containment : Intersection.None;\n    }\n\n    public static Intersection IntersectionOf(PointF point, Line line)\n    {\n        float bottomY = Math.Min(line.Y1, line.Y2);\n        float topY = Math.Max(line.Y1, line.Y2);\n        bool heightIsRight = point.Y &gt;= bottomY &amp;&amp;\n                             point.Y &lt;= topY;\n        //Vertical line, slope is divideByZero error!\n        if (line.X1 == line.X2)\n        {\n            if (point.X == line.X1 &amp;&amp; heightIsRight)\n            {\n                return Intersection.Tangent;\n            }\n            else\n            {\n                return Intersection.None;\n            }\n        }\n        float slope = (line.X2 - line.X1)/(line.Y2 - line.Y1);\n        bool onLine = (line.Y1 - point.Y) == (slope*(line.X1 - point.X));\n        if (onLine &amp;&amp; heightIsRight)\n        {\n            return Intersection.Tangent;\n        }\n        else\n        {\n            return Intersection.None;\n        }\n    }\n\n}\n</code></pre>\n'}, {'answer_id': 30126, 'author': 'Nathan DeWitt', 'author_id': 1753, 'author_profile': 'https://Stackoverflow.com/users/1753', 'pm_score': 1, 'selected': False, 'text': '<p>If it is 2d, then all lines are on the only plane.</p>\n\n<p>So, this is basic 3-D geometry. You should be able to do this with a straightforward equation.</p>\n\n<p>Check out this page: </p>\n\n<blockquote>\n  <p><a href="http://local.wasp.uwa.edu.au/~pbourke/geometry/planeline/" rel="nofollow noreferrer">http://local.wasp.uwa.edu.au/~pbourke/geometry/planeline/</a>. </p>\n</blockquote>\n\n<p>The second solution should be easy to implement, as long as you translate the coordinates of your rectangle into the equation of a plane.  </p>\n\n<p>Furthermore, check that your denominator isn\'t zero (line doesn\'t intersect or is contained in the plane).</p>\n'}, {'answer_id': 1119142, 'author': 'fortran', 'author_id': 106979, 'author_profile': 'https://Stackoverflow.com/users/106979', 'pm_score': -1, 'selected': False, 'text': '<p>I hate browsing the MSDN docs (they\'re awfully slow and weird :-s) but I think they should have something similar to <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/geom/Rectangle2D.html#intersectsLine%28double,%20double,%20double,%20double%29" rel="nofollow noreferrer">this Java method</a>... and if they haven\'t, bad for them! XD (btw, it works for segments, not lines).</p>\n\n<p>In any case, you can peek the open source Java SDK to see how is it implemented, maybe you\'ll learn some new trick (I\'m always surprised when I look other people\'s code)</p>\n'}, {'answer_id': 1593336, 'author': 'Jaguar', 'author_id': 109338, 'author_profile': 'https://Stackoverflow.com/users/109338', 'pm_score': 2, 'selected': False, 'text': "<p>since it is missing i'll just add it for completeness</p>\n\n<pre><code>public static Intersection IntersectionOf(Line line1, Line line2)\n    {\n        //  Fail if either line segment is zero-length.\n        if (line1.X1 == line1.X2 &amp;&amp; line1.Y1 == line1.Y2 || line2.X1 == line2.X2 &amp;&amp; line2.Y1 == line2.Y2)\n            return Intersection.None;\n\n        if (line1.X1 == line2.X1 &amp;&amp; line1.Y1 == line2.Y1 || line1.X2 == line2.X1 &amp;&amp; line1.Y2 == line2.Y1)\n            return Intersection.Intersection;\n        if (line1.X1 == line2.X2 &amp;&amp; line1.Y1 == line2.Y2 || line1.X2 == line2.X2 &amp;&amp; line1.Y2 == line2.Y2)\n            return Intersection.Intersection;\n\n        //  (1) Translate the system so that point A is on the origin.\n        line1.X2 -= line1.X1; line1.Y2 -= line1.Y1;\n        line2.X1 -= line1.X1; line2.Y1 -= line1.Y1;\n        line2.X2 -= line1.X1; line2.Y2 -= line1.Y1;\n\n        //  Discover the length of segment A-B.\n        double distAB = Math.Sqrt(line1.X2 * line1.X2 + line1.Y2 * line1.Y2);\n\n        //  (2) Rotate the system so that point B is on the positive X axis.\n        double theCos = line1.X2 / distAB;\n        double theSin = line1.Y2 / distAB;\n        double newX = line2.X1 * theCos + line2.Y1 * theSin;\n        line2.Y1 = line2.Y1 * theCos - line2.X1 * theSin; line2.X1 = newX;\n        newX = line2.X2 * theCos + line2.Y2 * theSin;\n        line2.Y2 = line2.Y2 * theCos - line2.X2 * theSin; line2.X2 = newX;\n\n        //  Fail if segment C-D doesn't cross line A-B.\n        if (line2.Y1 &lt; 0 &amp;&amp; line2.Y2 &lt; 0 || line2.Y1 &gt;= 0 &amp;&amp; line2.Y2 &gt;= 0)\n            return Intersection.None;\n\n        //  (3) Discover the position of the intersection point along line A-B.\n        double posAB = line2.X2 + (line2.X1 - line2.X2) * line2.Y2 / (line2.Y2 - line2.Y1);\n\n        //  Fail if segment C-D crosses line A-B outside of segment A-B.\n        if (posAB &lt; 0 || posAB &gt; distAB)\n            return Intersection.None;\n\n        //  (4) Apply the discovered position to line A-B in the original coordinate system.\n        return Intersection.Intersection;\n    }\n</code></pre>\n\n<p>note that the method rotates the line segments so as to avoid direction-related problems</p>\n"}, {'answer_id': 1599886, 'author': 'Imran', 'author_id': 182306, 'author_profile': 'https://Stackoverflow.com/users/182306', 'pm_score': -1, 'selected': False, 'text': "<p>Isn't it possible to check the line against each side of the rectangle using simple line segment formula.</p>\n"}, {'answer_id': 9686237, 'author': 'lestival', 'author_id': 239483, 'author_profile': 'https://Stackoverflow.com/users/239483', 'pm_score': 1, 'selected': False, 'text': '<p>Use class:</p>\n\n<pre><code>System.Drawing.Rectangle\n</code></pre>\n\n<p>Method:</p>\n\n<pre><code>IntersectsWith();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/30080', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/623/']