[Math] What’s the equation for a rectircle? (Perfect rounded-corner rectangle without stretching on only one dim)

algebra-precalculusgeometrymath-software

The equation for a rounded square seems to be:
$x^4 + y^4 = 1$

You can make the radii smaller by increasing (over the even integers) the exponents in the equation.

Here's a picture:
Wolfram Alpha squircle.

If you try to make a rectangle out of it by simply throwing in constant scalars:
$(x/a)^4 + (x/b)^4 = 1$

You do get a more typical rectangular shape:
naively rectangularized squircle

However the round corners seemed to be stretched on the $x$-axis and non-stretched on the $y$-axis. In other words it doesn't look like a typical rounded rectangle composed by rounding (with a circle, non-stretched on either dim) a rectangle:
Google search: rounded rectangle.

So I'm wondering how you'd modify the squircle equation to get flat tops, flat sides, except circles on the corners – not circles stretched on one dimension.

What I've tried:
adding terms to the equation and looking at the produced image on Wolfram alpha.

Any idea how to make a perfect rounded rectangle with an equation?

Best Answer

A rounded rectangle of size $2a\times2b$ with rounding radius $r$ is given by $$f(x;a,r) + f(y;b,r) = 1$$ where $$f(x;a,r)=\begin{cases}\left(\frac{|x|-(a-r)}r\right)^2&\text{if $|x|\ge a-r$,}\\0&\text{otherwise.}\end{cases}$$ You want to approximate this with some function of the form $(|x|/a)^p$. Compare derivatives at $x=a$ and you get $p=2a/r$. So a "rectircle" of size $2a\times2b$ with rounding radius $r$ is given by $$\left(\frac{|x|}a\right)^{2a/r} + \left(\frac{|y|}b\right)^{2b/r}=1.$$

enter image description here

enter image description here