[Math] distance to the edge of a rounded rectangle

geometry

I'm trying to figure out a formula to create a rounded rectangle… but I need to do it by finding the distance from $(0, 0)$ to the line/edge of the rectangle given the angle provided as a vector.

Ok.. so here is the formula to plot the rectangle: $\left(\frac{x}{a}\right)^n + \left(\frac{y}{b}\right)^n = 1$

In my usage a and b will both be 1. Thus the formula can be simplified into $x^n + y^n = 1$

http://www.wolframalpha.com/input/?i=x%5E4+%2B+y%5E4+%3D+1

Rounded Rectangle Plot

My input will be a point in space that can represent a vector. Say it's (.5,.5). The angle of this vector is $45^\circ$ and therefore it intersects with the plotted line at a specific distance from $(0, 0)$. I don't need to know the $x$, $y$ of the plot per se. I need to know the distance to that point. If I have the x,y I can easily calculate the distance. But maybe there is a way to calculate the distance directly.

I know I have: $\left(\frac{x}{a}\right)^n + \left(\frac{y}{b}\right)^n = 1$

and I have $d = \sqrt{x^2 + y^2}$

But alone that isn't enough info for me to figure it out. I can see a pattern in there though. OH crap they're the same formula, except that $n=2$, where I want $n=4$ (or $n>4$). When $d = 1$ the square root of $1 = 1$.

I was able to solve for d like this – but I don't know if this is helping…

1 = x^n + y^n

0 = x^n + y^n – 1

d = sqr(x^2 + y^2)

0 = sqr(x^2 + y^2) – d

d = 1 + sqr(x^2 + y^2) – x^n – y^n

But still.. I don't know x and y on the plot. I'm given x,y in space and have to find the distance to the plotted line where it intersects with that vector.

Maybe rather I need some sort of intersection type formula?? Any ideas?

EDIT —-

Really what I need is to figure out the distance to the line at any given angle.

EDIT 2 —

What about this approach..

Here are the knowns.. I have an x,y out in space. Therefore I have an angle and a distance for that vector. I also know the base size of the rounded rectangle.

If it was a circle it's easy cause the radius of a circle is constant.

Circle vs rounded rectangle

My answer here might not be very mathematical in terms of equation syntax.. but I'm a programmer, not a mathematician..

It should be possible to calculate the difference between the radius of the circle (known if the object were a circle) and the "radius" at any point on the rounded rectangle.

At an angle of $\theta$ they are the same. At and angle of 90 they are the same. But at points between the two they are not.

If the radius of the circle is 1.. then you have $d = 1 + \sqrt{x^2 + y^2} – x^n – y^n$ for the "radius" or distance to the edge of the rounded rectangle.

The difference between the two is delta $d = 1 – x^n – y^n$

So if our desired rectangle is S in size instead of 1 – S is a scale factor over the 1.

If I normalize the vector $(x, y)$ and make it's length 1. Then use that x, y to figure out the delta d. I can then scale that dd by S and add it to d to get the distance at that angle.

Checking to see if it works in code.

Best Answer

Let the vector be $(a,b)$. The line it is on is $y=\frac ba x$ (assuming it is not vertical.) Plug this into your superellipse and get $x^n+\left(\frac ba x\right)^n=1$ or $x^n= \frac 1{1+(\frac ba)^n}=\frac {a^n}{a^n+b^n}, x=\left(\frac {a^n}{a^n+b^n}\right)^{\frac 1n}$ for the intersection point.

Related Question