Q:

Create waypoints on a polygon with JS

This is more a mathematical than a JS problem, but I hope you can help me.
I'm trying to create waypoints around a polygon. It should be able to walk on a line. I need to observe the following cases (true means the way isn't possible):

 
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 jsFiddle.
 
// check if point is in polygon
var intersectLinePolygon = function(A, B, poly){
  var result = false;
  for (var i = 0; i < 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 && B.y == C.y) || 
    (B.x == D.x && B.y == D.y) ||
    (A.x == C.x && A.y == C.y) ||
    (A.x == D.x && A.y == D.y)
){ return false; }
  else { return (ccw(A,C,D) != ccw(B,C,D) && 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) > (B.y-A.y)*(C.x-A.x)); };

A:

I could solve it with using poly2tri and wrote a new approach.
https://code.google.com/p/poly2tri/

