Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's possible to check if a point is inside a sector with only integer arithmetic and the basic operations of addition, subtraction and multiplication.</p> <p>For a point to be inside a <a href="http://en.wikipedia.org/wiki/Circular_sector" rel="noreferrer">circular sector</a>, it has to meet the following tests:</p> <ol> <li>It has to be positioned counter-clockwise from the start "arm" of the sector.</li> <li>It has to be positioned clockwise from the end arm of the sector.</li> <li><p>It has to be closer to the center of the circle than the sector's radius.</p> <p><img src="https://i.stack.imgur.com/jMHJw.png" alt="For a point to be inside a, it has to meet the following tests"> <img src="https://i.stack.imgur.com/YrKuj.png" alt="It has to be positioned counter-clockwise from the start &quot;arm&quot; of the sector"> <img src="https://i.stack.imgur.com/c1gsP.png" alt="It has to be positioned clockwise from the end arm of the sector"> <img src="https://i.stack.imgur.com/lofmc.png" alt="It has to be closer to the center of the circle than the sector&#39;s radius"></p></li> </ol> <h2>Clockwise tests</h2> <p>To test if a vector v2 is clockwise to a another vector v1, do the following:</p> <ol> <li><p>Find the counter-clockwise <a href="http://en.wikipedia.org/wiki/Normal_%28geometry%29" rel="noreferrer">normal vector</a> of v1. The normal vector is at a 90 degrees angle to the original vector. This is <a href="https://stackoverflow.com/questions/1243614/how-do-i-calculate-the-normal-vector-of-a-line-segment">straightforward to do</a>: if <code>v1=(x1,y1)</code>, then the counter-clockwise normal is <code>n1=(-y1,x1)</code>.</p></li> <li><p>Find the size of the projection of v2 on the normal. This can be done by calculating the <a href="http://en.wikipedia.org/wiki/Dot_product" rel="noreferrer">dot product</a> of v2 and the normal.</p> <p><code>projection = v2.x*n1.x + v2.y*n1.y</code></p></li> <li><p>If the projection is a positive number, then the v2 is positioned counter-clockwise to v1. Otherwise, v2 is clockwise to v1.</p></li> </ol> <p>Here's a counter-clockwise example: <img src="https://i.stack.imgur.com/rG3JS.png" alt="Counter-clockwise example"></p> <p>And a clockwise example: <img src="https://i.stack.imgur.com/W0FPK.png" alt="Clockwise example"></p> <p>The steps can be combined: </p> <pre><code>function areClockwise(v1, v2) { return -v1.x*v2.y + v1.y*v2.x &gt; 0; } </code></pre> <h2>Radius test</h2> <p>The radius test is straightforward. Just check if the distance of the point from the center of the circle is less than the desired radius. To avoid computing square roots, we can compare the square of the distance with the square of the radius instead. </p> <pre><code>function isWithinRadius(v, radiusSquared) { return v.x*v.x + v.y*v.y &lt;= radiusSquared; } </code></pre> <h2>Putting it together</h2> <p>The complete sector test looks something like: </p> <pre><code>function isInsideSector(point, center, sectorStart, sectorEnd, radiusSquared) { var relPoint = { x: point.x - center.x, y: point.y - center.y }; return !areClockwise(sectorStart, relPoint) &amp;&amp; areClockwise(sectorEnd, relPoint) &amp;&amp; isWithinRadius(relPoint, radiusSquared); } </code></pre> <p>The following sample page demonstrates this over several thousand points. You can experiment with the code at: <a href="http://jsbin.com/oriyes/8/edit" rel="noreferrer">http://jsbin.com/oriyes/8/edit</a>.</p> <p><img src="https://i.stack.imgur.com/0PbMe.png" alt="Screenshot"></p> <h2>Sample source code</h2> <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="http://code.jquery.com/jquery-1.8.2.min.js"&gt;&lt;/script&gt; &lt;style&gt; .canvas { position: absolute; background: #f4f4f4; border: 8px solid #f4f4f4; width: 400px; height: 400px; } .dot { position: absolute; font: 16px Arial; } .out { color: #ddd; } .in { color: #00dd44; } &lt;/style&gt; &lt;script&gt; function isInsideSector(point, center, sectorStart, sectorEnd, radiusSquared) { var relPoint = { x: point.x - center.x, y: point.y - center.y }; return !areClockwise(sectorStart, relPoint) &amp;&amp; areClockwise(sectorEnd, relPoint) &amp;&amp; isWithinRadius(relPoint, radiusSquared); } function areClockwise(v1, v2) { return -v1.x*v2.y + v1.y*v2.x &gt; 0; } function isWithinRadius(v, radiusSquared) { return v.x*v.x + v.y*v.y &lt;= radiusSquared; } $(function() { var $canvas = $("#canvas"); var canvasSize = 400; var count = 4000; // define the sector var center = { x: canvasSize / 2, y: canvasSize / 2 }; var sectorStart = { x: 4, y: 1 }; var sectorEnd = { x: 1, y: 4 }; var radiusSquared = canvasSize * canvasSize / 4; // create, draw and test a number of random points for (var i = 0; i &lt; count; ++i) { // generate a random point var point = { x: Math.random() * canvasSize, y: Math.random() * canvasSize }; // test if the point is inside the sector var isInside = isInsideSector(point, center, sectorStart, sectorEnd, radiusSquared); // draw the point var $point = $("&lt;div class='dot'&gt;&lt;/div&gt;") .css({ left: point.x - 3, top: canvasSize - point.y - 8 }) .html("&amp;#8226;") .addClass(isInside ? "in" : "out") .appendTo($canvas); } }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="canvas" class="canvas"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>Notes, caveats and limitations</h2> <ol> <li><p>You have to specify the boundaries of the sector in terms of vectors. The screenshot above, for example, shows a sector stretching between the vectors of (4,1) and (1,4).</p></li> <li><p>If your sector is specified in other terms, e.g. angles, you will have to convert that to vectors first, e.g. using the <code>tan()</code> function. Fortunately, you only have to do this once.</p></li> <li><p>The logic here works for sectors with an inner angle of less than 180 degrees. If your sectors can span a larger angle, you'll have to modify it.</p></li> <li><p>Additionally, the code assumes that you know which of the bounding vectors of the sector is the "start" and which is the "end". If you don't, you can run the <code>areClockwise()</code> on them to find out.</p></li> <li><p>Note that while all this can be done with integer arithmetic, both the radius and clockwise tests use a larger range of numbers, due to squaring x's and y's and multiplying them together. Make sure to use integers of sufficient bits to hold the results.</p></li> </ol>
    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