interface Vec2Like { x : number ; y : number ; } export namespace Math2D { /**
     * Alternative 2D box object with less memory impact (four numbers instead of two min/max
     * objects with two numbers each). Should be faster.
     */ export class Box { param x param y param w param h constructor ( public x = 0 , public y = 0 , public w = 0 , public h = 0 ) { } param x param y param w param h set ( x , y , w , h ) { this . x = x ; this . y = y ; this . w = w ; this . h = h ; } param box copy ( box ) { this . x = box . x ; this . y = box . y ; this . w = box . w ; this . h = box . h ; } param x param y contains ( x , y ) { return this . x <= x && this . x + this . w >= x && this . y <= y && this . y + this . h >= y ; } param other containsBox ( other ) { const xmax = other . x + other . w ; const ymax = other . y + other . h ; return ( this . contains ( other . x , other . y ) && this . contains ( xmax , other . y ) && this . contains ( other . x , ymax ) && this . contains ( xmax , ymax ) ) ; } param other intersects ( other ) { return ( this . x <= other . x + other . w && this . x + this . w >= other . x && this . y <= other . y + other . h && this . y + this . h >= other . y ) ; } } /**
     * Box to store UV coordinates.
     */ export interface UvBox { s0 : number ; t0 : number ; s1 : number ; t1 : number ; } param ax param ay param bx param by returns export function distSquared ( ax , ay , bx , by ) { return ( ax - bx ) * ( ax - bx ) + ( ay - by ) * ( ay - by ) ; } param line export function computeSquaredLineLength ( line ) { let squaredLineLength = 0 ; const length = line . length - 4 ; for ( let i = 0 ; i < length ; i += 3 ) { const xDiff = line [ i + 3 ] - line [ i ] ; const yDiff = line [ i + 4 ] - line [ i + 1 ] ; squaredLineLength += xDiff * xDiff + yDiff * yDiff ; } return squaredLineLength ; } param px param py param l0x param l0y param l1x param l1y returns export function distToSegmentSquared ( px , py , l0x , l0y , l1x , l1y ) { const lineLengthSuared = distSquared ( l0x , l0y , l1x , l1y ) ; if ( lineLengthSuared === 0 ) { return distSquared ( px , py , l0x , l0y ) ; } let t = ( ( px - l0x ) * ( l1x - l0x ) + ( py - l0y ) * ( l1y - l0y ) ) / lineLengthSuared ; t = Math . max ( 0 , Math . min ( 1 , t ) ) ; return distSquared ( px , py , l0x + t * ( l1x - l0x ) , l0y + t * ( l1y - l0y ) ) ; } param xLine1 param yLine1 param xLine2 param yLine2 param radius param xCenter param yCenter returns export function intersectLineAndCircle ( xLine1 , yLine1 , xLine2 , yLine2 , radius , xCenter = 0 , yCenter = 0 ) { const epsilon = 1e-10 ; const dx = xLine2 - xLine1 ; const dy = yLine2 - yLine1 ; const dSq = dx * dx + dy * dy ; const rSq = radius * radius ; const c = xLine1 * yLine2 - xLine2 * yLine1 ; const cp = c - dy * xCenter + dx * yCenter ; const discriminantSquared = rSq * dSq - cp * cp ; if ( discriminantSquared < - epsilon ) { return undefined ; } const xMid = cp * dy ; const yMid = - cp * dx ; if ( discriminantSquared < epsilon ) { return { x1 : xCenter + xMid / dSq , y1 : yCenter + yMid / dSq } ; } const discriminant = Math . sqrt ( discriminantSquared ) ; const signDy = dy < 0 ? - 1 : 1 ; const absDy = Math . abs ( dy ) ; const xDist = signDy * dx * discriminant ; const yDist = absDy * discriminant ; return { x1 : xCenter + ( xMid + xDist ) / dSq , y1 : yCenter + ( yMid + yDist ) / dSq , x2 : xCenter + ( xMid - xDist ) / dSq , y2 : yCenter + ( yMid - yDist ) / dSq } ; } remarks This functions computes the
     *  https *    | line-line intersection} of two lines given two points on each line.
     *
     *  param x1 param y1 param x2 param y2 param x3 param y3 param x4 param y4 param result export function intersectLines ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 , result = { x : 0 , y : 0 } ) { const d = ( x1 - x2 ) * ( y3 - y4 ) - ( y1 - y2 ) * ( x3 - x4 ) ; if ( d === 0 ) { return undefined ; } const px = ( ( x1 * y2 - y1 * x2 ) * ( x3 - x4 ) - ( x1 - x2 ) * ( x3 * y4 - y3 * x4 ) ) / d ; const py = ( ( x1 * y2 - y1 * x2 ) * ( y3 - y4 ) - ( y1 - y2 ) * ( x3 * y4 - y3 * x4 ) ) / d ; result . x = px ; result . y = py ; return result ; } }