import { Point } from '../../models/point' ; import { Node } from '../../models/node' ; import { Direction } from '../../models/direction' ; export function mindNodeAnchors ( node ) { let r = borderRadius ( node ) ; let topN = 5 ; for ( let i = 0 ; i < topN ; i ++ ) { if ( i === 2 ) { continue ; } let x = node . rect . x + ( node . rect . width * ( i + 1 ) ) / ( topN + 1 ) ; let y = node . rect . y ; if ( x < node . rect . x + r ) { y = getYByCircle ( node . rect . x + r , y + r , x , r , - 1 ) ; } else if ( x > node . rect . x + node . rect . width - r ) { y = getYByCircle ( node . rect . x + node . rect . width - r , y + r , x , r , - 1 ) ; } node . anchors . push ( new Point ( x , y , Direction . Up ) ) ; } let rightN = 3 ; for ( let i = 0 ; i < rightN ; i ++ ) { let y = node . rect . y + ( node . rect . height * ( i + 1 ) ) / ( rightN + 1 ) ; let x = node . rect . x + node . rect . width ; if ( y < node . rect . y + r ) { x = getXByCircle ( x - r , node . rect . y + r , y , r ) ; } else if ( y > node . rect . y + node . rect . height - r ) { x = getXByCircle ( x - r , node . rect . y + node . rect . height - r , y , r ) ; } node . anchors . push ( new Point ( x , y , Direction . Right ) ) ; } let bottomN = 5 ; for ( let i = 0 ; i < bottomN ; i ++ ) { if ( i === 2 ) { continue ; } let x = node . rect . x + ( node . rect . width * ( i + 1 ) ) / ( bottomN + 1 ) ; let y = node . rect . y + node . rect . height ; if ( x < node . rect . x + r ) { y = getYByCircle ( node . rect . x + r , y - r , x , r ) ; } else if ( x > node . rect . x + node . rect . width - r ) { y = getYByCircle ( node . rect . x + node . rect . width - r , y - r , x , r ) ; } node . anchors . push ( new Point ( x , y , Direction . Bottom ) ) ; } let leftN = 3 ; for ( let i = 0 ; i < leftN ; i ++ ) { let y = node . rect . y + ( node . rect . height * ( i + 1 ) ) / ( leftN + 1 ) ; let x = node . rect . x ; if ( y < node . rect . y + r ) { x = getXByCircle ( x + r , node . rect . y + r , y , r , - 1 ) ; } else if ( y > node . rect . y + node . rect . height - r ) { x = getXByCircle ( x + r , node . rect . y + node . rect . height - r , y , r , - 1 ) ; } node . anchors . push ( new Point ( x , y , Direction . Left ) ) ; } } param node returns function borderRadius ( node ) { let wr = node . borderRadius ; let hr = node . borderRadius ; if ( node . borderRadius < 1 ) { wr = node . rect . width * node . borderRadius ; hr = node . rect . height * node . borderRadius ; } let r = wr < hr ? wr : hr ; if ( node . rect . width < 2 * r ) { r = node . rect . width / 2 ; } if ( node . rect . height < 2 * r ) { r = node . rect . height / 2 ; } return r ; } param ox param oy param y param r param sqrt function getXByCircle ( ox , oy , y , r , sqrt = 1 ) { return sqrt * Math . sqrt ( r ** 2 - ( y - oy ) ** 2 ) + ox ; } param ox param oy param y param r param sqrt function getYByCircle ( ox , oy , x , r , sqrt = 1 ) { return sqrt * Math . sqrt ( r ** 2 - ( x - ox ) ** 2 ) + oy ; }