Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are only two cases when the circle intersects with the rectangle:</p> <ul> <li>Either the circle's centre lies inside the rectangle, or</li> <li>One of the edges of the rectangle has a point in the circle.</li> </ul> <p>Note that this does not require the rectangle to be axis-parallel. </p> <p><a href="https://i.stack.imgur.com/fdDv1.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fdDv1.png" alt="Some different ways a circle and rectangle may intersect"></a></p> <p>(One way to see this: if none of the edges has a point in the circle (if all the edges are completely "outside" the circle), then the only way the circle can still intersect the polygon is if it lies completely inside the polygon.)</p> <p>With that insight, something like the following will work, where the circle has centre <code>P</code> and radius <code>R</code>, and the rectangle has vertices <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code> in that order (not complete code):</p> <pre class="lang-py prettyprint-override"><code>def intersect(Circle(P, R), Rectangle(A, B, C, D)): S = Circle(P, R) return (pointInRectangle(P, Rectangle(A, B, C, D)) or intersectCircle(S, (A, B)) or intersectCircle(S, (B, C)) or intersectCircle(S, (C, D)) or intersectCircle(S, (D, A))) </code></pre> <p>If you're writing any geometry you probably have the above functions in your library already. Otherwise, <code>pointInRectangle()</code> can be implemented in several ways; any of the general <a href="http://en.wikipedia.org/wiki/Point_in_polygon" rel="noreferrer">point in polygon</a> methods will work, but for a rectangle you can just check whether this works:</p> <pre><code>0 ≤ AP·AB ≤ AB·AB and 0 ≤ AP·AD ≤ AD·AD </code></pre> <p>And <code>intersectCircle()</code> is easy to implement too: one way would be to check if the foot of the perpendicular from <code>P</code> to the line is close enough and between the endpoints, and check the endpoints otherwise.</p> <p>The cool thing is that the <em>same</em> idea works not just for rectangles but for the intersection of a circle with any <a href="http://en.wikipedia.org/wiki/Simple_polygon" rel="noreferrer">simple polygon</a> — doesn't even have to be convex!</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