I'm writing an application where I need to be able to plot points onto a rounded rectangle.
I know the angle from the center from 0 degrees to where the point needs to be.
I need to know the x and y coordinates of where a line from this angle would meet the rectangle.
For a circle this would be something like
x = sin(angle) * radius + centerx
y = cos(angle) * radius + centery
What i need is the equivalent formula for doing this on a rectangle and ideally a rounded one.
Thanks
Best Answer
Is the next function convenient for you ? $$\left(\frac{x-x_0}{a}\right)^{2n}+\left(\frac{y-y_0}{b}\right)^{2n}=1$$
In polar coordinates with center $(x_0,y_0)$ : $\begin{cases} x=x_0+\rho\cos(\theta) \\ y=y_0+\rho\sin(\theta) \end{cases}$
$$\left(\frac{\cos(\theta)}{a}\right)^{2n}+\left(\frac{\sin(\theta)}{b}\right)^{2n}=\frac{1}{\rho^{2n}}$$
In order to draw a curve for given $n$ : $$\rho(\theta)=\frac{1}{\left(\left(\frac{\cos(\theta)}{a}\right)^{2n}+\left(\frac{\sin(\theta)}{b}\right)^{2n} \right)^{1/(2n)}}$$
NOTE :
More generally $\quad \left|\frac{x-x_0}{a}\right|^{\nu}+\left|\frac{y-y_0}{b}\right|^{\nu}=1\quad$ is a "superellipse" :
http://mathworld.wolfram.com/Superellipse.html
The particular case $\nu=4$ is called "rectellipse".