Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate waypoints on a polygon with JS
    primarykey
    data
    text
    <p>This is more a mathematical than a JS problem, but I hope you can help me.</p> <p>I'm trying to create waypoints around a polygon. It should be able to walk <strong>on</strong> a line. I need to observe the following cases (<em>true</em> means the way isn't possible):</p> <p><img src="https://i.stack.imgur.com/kf1VH.jpg" alt="polygon"> &nbsp; In my script, I have problems to observe case 2 and case 7 at the same time. The polygon is an array with 5 objects (points) in them. A and B are the points for the red line. Here you can find my <a href="http://jsfiddle.net/bwe5a/3/" rel="nofollow noreferrer">jsFiddle</a>. &nbsp;</p> <pre><code>// check if point is in polygon var intersectLinePolygon = function(A, B, poly){ var result = false; for (var i = 0; i &lt; poly.length; i++){ var C = { 'x':poly[i].x, 'y':poly[i].y }; var D = {}; // if it's not the last point, take the next if (i != poly.length-1){ D.x = poly[i+1].x; D.y = poly[i+1].y; } // if it's the last point, take the first else { D.x = poly[0].x; D.y = poly[0].y; } if (intersectLineLine(A, B, C, D)){ result = true; } } return result; }; // check if there is an intersection between two lines var intersectLineLine = function(A, B, C, D){ if ( (B.x == C.x &amp;&amp; B.y == C.y) || (B.x == D.x &amp;&amp; B.y == D.y) || (A.x == C.x &amp;&amp; A.y == C.y) || (A.x == D.x &amp;&amp; A.y == D.y) ){ return false; } else { return (ccw(A,C,D) != ccw(B,C,D) &amp;&amp; ccw(A,B,C) != ccw(A,B,D)); } }; // helper function for intersectLineLine var ccw = function(A, B, C){ return ((C.y-A.y)*(B.x-A.x) &gt; (B.y-A.y)*(C.x-A.x)); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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