Video solution with explanation: https://youtu.be/oDJQU9wxxRM
Thanks, I wrote the question XD
Your explanation is correct also code is correct ,
Your simplification for case 3 maximum distance is correct as you got max_dist=r1+r2+d
after simplification you wrote wrong formula which can be seen from time 12:42 to 12:45
Nice video though.