The distance between the nearest point of two circles.
See also: Circumference point — the edge points used here are circumference points along the line between centres.
dx = a.x - b.x
dy = a.y - b.y
distance = sqrt(dx * dx + dy * dy) - a.radius - b.radius
const circleAX = 0
const circleAY = 300
const circleARadius = 150
const circleBX = 300
const circleBY = 0
const circleBRadius = 100
// angle between two points
const aA = Math.atan2(circleBY - circleAY, circleBX - circleAX)
// point on circle A's edge facing circle B
const circAX = circleAX + Math.cos(aA) * circleARadius
const circAY = circleAY + Math.sin(aA) * circleARadius
// point on circle B's edge facing circle A
const aB = Math.atan2(circleAY - circleBY, circleAX - circleBX)
const circBX = circleBX + Math.cos(aB) * circleBRadius
const circBY = circleBY + Math.sin(aB) * circleBRadius
// distance between nearest edges
const dx = circleAX - circleBX
const dy = circleAY - circleBY
const distance = Math.sqrt(dx * dx + dy * dy) - circleARadius - circleBRadius