What would be the "definitive" formula to calculate chainstay length from sprocket size, chainring size and number of chain links? Before I started to write this answer, I thoroughly read this question, its answers and comments:

How do I calculate the diameter of a chainring from the number of teeth?

So, my question tries to ellaborate over this, considering that, for single speeds, there will always be a certain unique chainlength (for a new drivetrain) given the gear sizes and the number of links in the chain. Also, for vertical dropouts, where chainstay length is fixed, there might be a "magical gear" when you have almost zero chain-slack with the right combination of sprocket teeth number, chainring teeth number, and number of chain links.

For that reason, I plan to write some app (html+javascript) to get this length. There is a program which does exactely that, but it is not possible, as far as I know, to know which formula it uses, or if it is actually correct:

http://eehouse.org/fixin/formfmu.php

Then, I think the following assumptions should be made:


The chain perimeter is the sum of the distances between the consecutive centerlines of each pin;
The nominal distance between centerlines, in a new chain, is half an inch, or "exactely" 12.7mm;
The chain is considered to run a straight line between the tangent points where it touches each gear (sprocket or chainring);
The chain is considered a regular polygon sector along the circumference of the gears, as pointed by Lantius in his answer to the linked question.


The result would be the answer to the question: "If I wrap a closed chain with N links around a spocket with S teeth and a chainring with C teeth and pull those apart, what would be the distance between the centerlines of the cog and the chainring?"

EDIT: I'm including an "updated" drawing I generated programmatically (it's quite involving!), so it would be easier to analyze the geometry of the problem:



X is the chainstay length, and it is the hypotenuse of the purple triangle, the short cathetus being R-r (known), and the longer cathetus being equal to B (unknown).

EDIT: (added trig formula) The whole length L of the chain (known) is equal to A+B+C+D. Since B = D, then L = 2B + A + C, or, with all the added trig:



The unknown (B) appears in both sides of the equation, and it looks like it cannot be isolated.

(To make matters worse, there are the natural limitations that teeth profile is not always the same between brands, there might be very small variations of link length between different chains even of the same brand (happens to me quite often), how much chainslack is actually necessary is debatable, and so on, but it would be nice to have a "canonical formula" which actually considers the physical nature of the pinion-and-chain engagement and allows us to calculate this, at least theoretically.)

Thanks for reading!
Unfortunately my crummy draw tool won't let me place the lines very accurately, but this is the idea:



The radii describe right angles to the two chain lines.  I'm pretty sure the two wrap angles on the two sprockets will add to 360 degrees.  The upper quadrilateral has been divided into a rectangle (if my lines were square) and a right triangle.  The hypotenuse of the triangle is the chain stay length.  The short side is the large sprocket radius minus the small sprocket radius.  

From the chain stay length and the sprocket radii you can compute the straight chain length and (with a bit of trig) the two wrap angles.  Given a wrap angle and a sprocket radius you can compute the amount of chain on the sprocket.