Sorry, I was booted into Vista at the time and didn't have my copy of Photoshop available to draw a picture of how I derived that formula.
I'll try to explain it in text...
Consider just the end circle with radius R. There is a horizontal chord which is distance D from the bottom of the circle. We wish to find
the length of this chord.
Now consider these three points: the center of the circle, and the two endpoints of the chord (which lie on the circle as well). They form an isoscles triangle, with the base of the triange being the chord, and the two other sides being equal to the radius R of the circle.
Now draw a vertical line through the center of the circle. It will bisect the chord, and hence bisect the isocsles triangle into two congruent right triangles.
The base of each right triangle is exactly half of the chord length. The height of the right triangles is equal to the radius R minus the depth D (if D < R), or the depth D minus the radius R (if D>R). The hyptonenuse of each right triangle is simply the radius of the circle R.
Hence, if C is the chord length, then by the Pythagorean Theorem we must have ( C / 2 ) ^ 2 + (D - R) ^2 = R^2. Solve this equation for C, and you'll have your chord length.