example/bundle.js
// calculate distance between a point and a line (segment)
calculate distance between a point and a line (segment)
calculate distance between a point and a line ( segment )
function distancePL(x, y, x1, y1, dx1, dy1, len1 ) {
        if( !len1 ) 
            len1 = -1
        var t = dx1 * dx1 + dy1 * dy1
        if( t == 0 )
            return null
        else {
            t = ( x * dy1 - y * dx1 - x1 * dy1 + y1 * dx1 ) / t
            if( Math.abs(dx1) > Math.abs(dy1) )
                var s = ( x - x1 - t * dy1 ) / dx1
            else
                var s = ( y - y1 + t * dx1 ) / dy1
            if( ( s >= 0 && s <= len1 ) || len1 < 0 )
                return {
                    s: s,
                    t: t,
                    x: x1 + s * dx1,
                    y: y1 + s * dy1,
                    dist: Math.abs(t)
                }
            else if( s < 0 ) {
                var dist = distance(x, y, x1, y1)
                return {
                    s: s,
                    dist: dist
                }
            } else {
                var dist = distance(x, y,
                                    x1 + len1*dx1,
                                    y1 + len1*dy1)
                return {
                    s: s,
                    dist: dist
                }
            }
        }
    }
function distancePL ( x , y , x1 , y1 , dx1 , dy1 , len1 ) { if ( ! len1 ) len1 = - 1 var t = dx1 * dx1 + dy1 * dy1 if ( t == 0 ) return null else { t = ( x * dy1 - y * dx1 - x1 * dy1 + y1 * dx1 ) / t if ( Math . abs ( dx1 ) > Math . abs ( dy1 ) ) var s = ( x - x1 - t * dy1 ) / dx1 else var s = ( y - y1 + t * dx1 ) / dy1 if ( ( s >= 0 && s <= len1 ) || len1 < 0 ) return { s : s , t : t , x : x1 + s * dx1 , y : y1 + s * dy1 , dist : Math . abs ( t ) } else if ( s < 0 ) { var dist = distance ( x , y , x1 , y1 ) return { s : s , dist : dist } } else { var dist = distance ( x , y , x1 + len1 * dx1 , y1 + len1 * dy1 ) return { s : s , dist : dist } } } }
calculate distance between a point and a line (segment)
calculate distance between a point and a line ( segment )
// in case length is not provided, assume a line // x = x1 + s * dx1 + t * dy1 // y = y1 + s * dy1 - t * dx1 // x * dy1 - y * dx1 = x1 * dy1 - y1 * dx1 + //                     t * ( dy1 * dy1 + dx1 * dx1 )