public WiresMagnet [ ] getMagnetsOnAutoConnection ( final WiresShape headS , final WiresShape tailS ) { final WiresConnection headC = getHeadConnection ( ) ; final WiresConnection tailC = getTailConnection ( ) ; if ( ! ( headC . isAutoConnection ( ) || tailC . isAutoConnection ( ) ) ) { // at least one side must be connected with auto connection on return null ; } WiresMagnet [ ] magnets ; final BoundingBox headBox = ( headS != null ) ? headS . getGroup ( ) . getComputedBoundingPoints ( ) . getBoundingBox ( ) : null ; final BoundingBox tailBox = ( tailS != null ) ? tailS . getGroup ( ) . getComputedBoundingPoints ( ) . getBoundingBox ( ) : null ; if ( getLine ( ) . getPoint2DArray ( ) . size ( ) > 2 ) { magnets = getMagnetsWithMidPoint ( headC , tailC , headS , tailS , headBox , tailBox ) ; } else { if ( ( headBox != null ) && ( tailBox != null ) && ! headBox . intersects ( tailBox ) ) { magnets = getMagnetsNonOverlappedShapes ( headS , tailS , headBox , tailBox ) ; } else { magnets = getMagnetsOverlappedShapesOrNoShape ( headC , tailC , headS , tailS , headBox , tailBox ) ; } } return magnets ; }
This is making some assumptions that will have to be fixed for anythign other than 8 Magnets at compass ordinal points . If there is no shape overlap and one box is in the corner of the other box then use nearest corner connections else use nearest mid connection . Else there is overlap . This is now much more difficult so just pick which every has the the shortest distanceto connections not contained by the other shape .