export class PonlygonUtils { param a param b param c public static dotV2 ( v1 , v2 ) { return v1 . x * v2 . x + v1 . y * v2 . y ; } param axis param polyArr returns public static calcProj ( axis , polyArr ) { let v = { "x" : polyArr [ 0 ] , "y" : polyArr [ 1 ] } ; let d , min , max ; min = max = this . dotV2 ( axis , v ) ; for ( let i = 2 ; i < polyArr . length - 1 ; i += 2 ) { v . x = polyArr [ i ] ; v . y = polyArr [ i + 1 ] ; d = this . dotV2 ( axis , v ) ; min = ( d < min ) ? d : min ; max = ( d > max ) ? d : max ; } return [ min , max ] ; } param min1 param max1 param min2 param max2 returns public static segDist ( min1 , max1 , min2 , max2 ) { if ( min1 < min2 ) { return min2 - max1 ; } else { return min1 - max2 ; } } param p1 param p2 returns public static isCollide ( p1 , p2 ) { if ( p1 . length < 6 || p2 . length < 6 ) { return false ; } let e = { "x" : 0 , "y" : 0 } ; let p = p1 , idx = 0 , len1 = p1 . length , len2 = p2 . length , px , py ; for ( let i = 0 , len = len1 + len2 ; i < len - 1 ; i += 2 ) { idx = i ; if ( i > len1 ) { p = p2 ; idx = ( i - len1 ) ; } if ( i === p . length - 2 ) { px = p [ 0 ] - p [ idx ] ; py = p [ 1 ] - p [ idx + 1 ] ; } else { px = p [ idx + 2 ] - p [ idx ] ; py = p [ idx + 3 ] - p [ idx + 1 ] ; } e . x = - py ; e . y = px ; let pp1 = this . calcProj ( e , p1 ) ; let pp2 = this . calcProj ( e , p2 ) ; if ( this . segDist ( pp1 [ 0 ] , pp1 [ 1 ] , pp2 [ 0 ] , pp2 [ 1 ] ) > 0 ) { return false ; } } return true ; } }