function distance_to_poly(point, poly) {
  var dists = $.map(poly, function(vertex1, i) {
    var prev = (i == 0 ? poly.length : i) - 1,
        vertex2 = poly[prev],
        edge = vsub(vertex2, vertex1);

    if (vlen(edge) == 0)
      return vlen(vsub(point, vertex1));

    // point + normal * i == vertex1 + edge * j
    var norm = vflipY(edge),
        x1 = point[0],
        x2 = norm[0],
        x3 = vertex1[0],
        x4 = edge[0],
        y1 = point[1],
        y2 = norm[1],
        y3 = vertex1[1],
        y4 = edge[1],
        i, j;

    if (y2 != 0) {
      j = (x3 - x1 - x2 * y3 / y2 + x2 * y1 / y2) / (x2 * y4 / y2 - x4);
      i = (y3 + j * y4 - y1) / y2;
    }
    else if (x2 != 0) { // normal can't be zero unless the edge has 0 length
      j = (y3 - y1 - y2 * x3 / x2 + y2 * x1 / x2) / (y2 * x4 / x2 - y4);
      i = (x3 + j * x4 - x1) / x2;
    }          

    if (j < 0 || j > 1 || j == null)
      return Math.min(
        vlen(vsub(point, vertex1)),
        vlen(vsub(point, vertex2)));

    return vlen(vscale(norm, i));
  });

  return Math.min.apply(null, dists);
}
/*
solution by @culebron
turn poly edge into a vector.
the edge vector scaled by j and its normal vector scaled by i meet
if the edge vector points between the vertices,
then normal is the shortest distance.
--------
x1 + x2 * i == x3 + x4 * j
y1 + y2 * i == y3 + y4 * j
0 < j < 1
--------

i == (y3 + j y4 - y1) / y2
x1 + x2 y3 / y2 + j x2 y4 / y2 - x2 y1 / y2 == x3 + j x4
j x2 y4 / y2 - j x4 == x3 - x1 - x2 y3 / y2 + x2 y1 / y2
j = (x3 - x1 - x2 y3 / y2 + x2 y1 / y2) / (x2 y4 / y2 - x4)
i = (y3 + j y4 - y1) / y2

i == (x3 + j x4 - x1) / x2
y1 + y2 x3 / x2 + j y2 x4 / x2 - y2 x1 / x2 == y3 + j y4
j y2 x4 / x2 - j y4 == y3 - y1 - y2 x3 / x2 + y2 x1 / x2
j = (y3 - y1 - y2 x3 / x2 + y2 x1 / x2) / (y2 x4 / x2 - y4)
i = (x3 + j x4 - x1) / x2
*/
solution by @culebron
turn poly edge into a vector.
the edge vector scaled by j and its normal vector scaled by i meet
if the edge vector points between the vertices,
then normal is the shortest distance.






solution by @culebron turn poly edge into a vector . the edge vector scaled by j and its normal vector scaled by i meet if the edge vector points between the vertices then normal is the shortest distance .
function distance_to_poly(point, poly) {
  var dists = $.map(poly, function(vertex1, i) {
    var prev = (i == 0 ? poly.length : i) - 1,
        vertex2 = poly[prev],
        edge = vsub(vertex2, vertex1);
    if (vlen(edge) == 0)
      return vlen(vsub(point, vertex1));
    var norm = vflipY(edge),
        x1 = point[0],
        x2 = norm[0],
        x3 = vertex1[0],
        x4 = edge[0],
        y1 = point[1],
        y2 = norm[1],
        y3 = vertex1[1],
        y4 = edge[1],
        i, j;
    if (y2 != 0) {
      j = (x3 - x1 - x2 * y3 / y2 + x2 * y1 / y2) / (x2 * y4 / y2 - x4);
      i = (y3 + j * y4 - y1) / y2;
    }
    else if (x2 != 0) { 
      j = (y3 - y1 - y2 * x3 / x2 + y2 * x1 / x2) / (y2 * x4 / x2 - y4);
      i = (x3 + j * x4 - x1) / x2;
    }          
    if (j < 0 || j > 1 || j == null)
      return Math.min(
        vlen(vsub(point, vertex1)),
        vlen(vsub(point, vertex2)));
    return vlen(vscale(norm, i));
  });
  return Math.min.apply(null, dists);
}
function distance_to_poly ( point , poly ) { var dists = $ . map ( poly , function ( vertex1 , i ) { var prev = ( i == 0 ? poly . length : i ) - 1 , vertex2 = poly [ prev ] , edge = vsub ( vertex2 , vertex1 ) ; if ( vlen ( edge ) == 0 ) return vlen ( vsub ( point , vertex1 ) ) ; var norm = vflipY ( edge ) , x1 = point [ 0 ] , x2 = norm [ 0 ] , x3 = vertex1 [ 0 ] , x4 = edge [ 0 ] , y1 = point [ 1 ] , y2 = norm [ 1 ] , y3 = vertex1 [ 1 ] , y4 = edge [ 1 ] , i , j ; if ( y2 != 0 ) { j = ( x3 - x1 - x2 * y3 / y2 + x2 * y1 / y2 ) / ( x2 * y4 / y2 - x4 ) ; i = ( y3 + j * y4 - y1 ) / y2 ; } else if ( x2 != 0 ) { j = ( y3 - y1 - y2 * x3 / x2 + y2 * x1 / x2 ) / ( y2 * x4 / x2 - y4 ) ; i = ( x3 + j * x4 - x1 ) / x2 ; } if ( j < 0 || j > 1 || j == null ) return Math . min ( vlen ( vsub ( point , vertex1 ) ) , vlen ( vsub ( point , vertex2 ) ) ) ; return vlen ( vscale ( norm , i ) ) ; } ) ; return Math . min . apply ( null , dists ) ; }
solution by @culebron
turn poly edge into a vector.
solution by @culebron turn poly edge into a vector .
// point + normal * i == vertex1 + edge * j // normal can't be zero unless the edge has 0 length