export interface Point { left : number ; top : number ; } export interface Rect { left : number ; right : number ; top : number ; bottom : number ; } export function pointInsideRect ( point , rect ) { return point . left >= rect . left && point . left < rect . right && point . top >= rect . top && point . top < rect . bottom ; } export function intersectRects ( rect1 , rect2 ) { let res = { left : Math . max ( rect1 . left , rect2 . left ) , right : Math . min ( rect1 . right , rect2 . right ) , top : Math . max ( rect1 . top , rect2 . top ) , bottom : Math . min ( rect1 . bottom , rect2 . bottom ) , } ; if ( res . left < res . right && res . top < res . bottom ) { return res ; } return false ; } export function translateRect ( rect , deltaX , deltaY ) { return { left : rect . left + deltaX , right : rect . right + deltaX , top : rect . top + deltaY , bottom : rect . bottom + deltaY , } ; } export function constrainPoint ( point , rect ) { return { left : Math . min ( Math . max ( point . left , rect . left ) , rect . right ) , top : Math . min ( Math . max ( point . top , rect . top ) , rect . bottom ) , } ; } export function getRectCenter ( rect ) { return { left : ( rect . left + rect . right ) / 2 , top : ( rect . top + rect . bottom ) / 2 , } ; } export function diffPoints ( point1 , point2 ) { return { left : point1 . left - point2 . left , top : point1 . top - point2 . top , } ; }