# Formula for a Stadium shape (2D Capsule)?

(Known as a stadium, it's basically a 2D capsule.)

I need a formula to draw this shape on a graph, preferably in that orientation (on its side).

What I've tried:

I've tried adapting the formula for a capsule from 3D to 2D, but to no avail, I'm only able to produce a circle from it. So not really sure how to approach this problem as I've not tried mapping any shape like this to a function before.

Context:

Not sure how relevant this is to the question, but I'm experimenting with different camera speed-up and slow-down functions for an RTS game I'm making, and in order to tell the camera how to move, I'm trying different functions to map its speed (y axis) with time (x axis), as it is told to move or ceases being told to move. One such method I want to try is, if you divide the stadium shape into 6 sections, one section being the top left curve part, another being the bottom left curve part, another being the top middle part, etc, mapping the top left curve part followed by the top middle part as the speed time graph of the camera for acceleration, and the top middle part and the top right part for deceleration, which I could achieve by taking the magnitude of the formula to remove the bottom half.

I could also just use the equation for a circle and isolate the top left, and once it reaches the peak just keep it at that value until the input is let go of, and then switch to the top right, but in searching for the stadium formula I found that it doesn't seem to be anywhere online, therefore this question would likely be useful to others searching for it.

How's this?

$$\left(x-\frac{a}{2}-\sqrt{r^2-y^2}\right)\left(x+\frac{a}{2}+\sqrt{r^2-y^2}\right)(y-r)(y+r)=10^{-n}$$

$$a$$ and $$r$$ here are exactly the same parameters as in the diagram you included. $$n$$ is a "quality factor" of sorts; the larger it gets, the sharper the "corners" of the stadium get (i.e. the points $$(\pm\frac{a}{2},\pm a)$$). When $$n=5$$, the graph is nearly indistinguishable from when $$n=\infty$$ (i.e. when the right side of the equation is $$0$$).

Here's the Desmos page I used to make the graph: https://www.desmos.com/calculator/1hjfojqisv

When $$a=r=4$$ and $$n=5$$, the graph of this equation looks like this:

• Could you please show the derivation of that formula? Commented Aug 10, 2023 at 4:42
• @Unknown123 I didn't really derive that formula; I just played around in Desmos until I found something that worked. Each term in parentheses, when set equal to 0, produces one of the four parts of the figure -- either a circular arc or a horizontal line. Multiplying the terms together trims the horizontal lines at the points where they intersect the arcs for reasons I might have understood 4 years ago but don't today. For more reasons I don't remember, when the right-hand side is 0, the line segments disappear. But making it close to zero (i.e. $10^{-5}$) achieves the desired affect. Commented Aug 12, 2023 at 5:51
• @RobertHoward thanks for this note, I was actually looking for the derivation and found nothing online. Commented Jun 13 at 23:37