Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would recommend simply doing a line-segment-line-segment intersection check on each line segment (edge) that makes up the rectangle. Here is a line segment intersection detection algorithm I wrote ages ago, dredged up from one of my old XNA projects:</p> <pre><code>// a1 is line1 start, a2 is line1 end, b1 is line2 start, b2 is line2 end static bool Intersects(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2, out Vector2 intersection) { intersection = Vector2.Zero; Vector2 b = a2 - a1; Vector2 d = b2 - b1; float bDotDPerp = b.X * d.Y - b.Y * d.X; // if b dot d == 0, it means the lines are parallel so have infinite intersection points if (bDotDPerp == 0) return false; Vector2 c = b1 - a1; float t = (c.X * d.Y - c.Y * d.X) / bDotDPerp; if (t &lt; 0 || t &gt; 1) return false; float u = (c.X * b.Y - c.Y * b.X) / bDotDPerp; if (u &lt; 0 || u &gt; 1) return false; intersection = a1 + t * b; return true; } </code></pre> <p>I will leave inputting each edge into the above method and collecting the results as an exercise to the reader :)</p> <hr> <h2>Edit 1 year later now I've gone to university and done a Graphics course:</h2> <p>Take a look at the <a href="http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm" rel="noreferrer">Cohen–Sutherland algorithm</a> to do this efficiently when you have a large set of lines where most do not intersect the rectangle. It uses a 9 segment grid and you place each endpoint of the line in a region of said grid:</p> <p><img src="https://i.stack.imgur.com/YcU1P.gif" alt="grid"></p> <p>Using this we can tell if there will not be any line intersections:</p> <p><img src="https://i.stack.imgur.com/JXMEh.jpg" alt="grid with lines"></p> <p>For example here <code>CD</code> will not intersect the rectangle (shown in red in the first image) as both <code>C</code> and <code>D</code> are in the top row and neither will <code>AB</code>. For the ones where the line may intersect the the rectangle we have to try the line-line intersections.</p> <p>They way the sections are numbered/labelled allows us to simply do <code>x AND y != 0</code> (where <code>x</code> and <code>y</code> are the labels of the sections for each of the line's endpoints) to determine if there will not be an intersection.</p> <p>Using this method means we have to many, many fewer line-line intersections which speeds up the whole thing massively.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload