inquire 'Enter first circle centre coordinates and radius: cx1, cy1, r1' x1 y1 r1
inquire 'Enter second circle centre coordinates and radius: cx2, cy2, r2' x2 y2 r2
dsq = (x2-x1)^2+(y2-y1)^2
d = sqrt(dsq)
if (d>=r1+r2) then
 display `no intersection'
 return
endif
r = 0.5*(r2+r1-d)
if ( r>=r2 ) then
  r = r2
  a = x2
  b = y2
  goto draw
endif
if ( r>=r1 ) then
  r = r1
  a = x1
  b = y1
  goto draw
endif
!a = 0.5*(r1*r1-r2*r2)*(x2-x1)/dsq + 0.5*(x2+x1)
!b = 0.5*(r1*r1-r2*r2)*(y2-y1)/dsq + 0.5*(y2+y1)
a = (d-r2+r)*(x2-x1)/d+x1
b = (d-r2+r)*(y2-y1)/d+y1
draw:
generate theta 1,,360 100
clear
window 0
defaults
set autoscale commensurate
graph\-axes r1*cosd(theta)+x1 r1*sind(theta)+y1
set plotsymbol -1
set plotsymbolcolor red
graph\overlay x1 y1
set plotsymbol 0
set curvecolor red
graph\overlay r1*cosd(theta)+x1 r1*sind(theta)+y1
set plotsymbol -1
set plotsymbolcolor blue
graph\overlay x2 y2
set plotsymbol 0
set curvecolor blue
graph\overlay r2*cosd(theta)+x2 r2*sind(theta)+y2
set plotsymbol -1
set plotsymbolcolor green
graph\overlay a b
set plotsymbol 0
set curvecolor green
graph\overlay r*cosd(theta)+a r*sind(theta)+b
!replot
display `Largest circle in the intersection: center= ('//rchar(a)//`,'//rchar(b)//`) radius= '//rchar(r)

