Equation of a rectangle

I need to graph a rectangle on the Cartesian coordinate system. Is there an equation for a rectangle? I can't find it anywhere.


Solution 1:

Based on Raskolnikov's answer here, one can build an implicit Cartesian equation for a $2p \times 2q$ rectangle:

$$\left(\frac{x}{p}\right)^2+\left(\frac{y}{q}\right)^2=\sec\left(\arctan\left(\frac{x}{p},\frac{y}{q}\right)-\frac{\pi}{2}\left\lfloor\frac2{\pi}\arctan\left(\frac{x}{p},\frac{y}{q}\right)+\frac12\right\rfloor\right)^2$$

Another one is based on modifying the implicit equation of a Lamé curve:

$$\left|\frac{x}{p}+\frac{y}{q}\right|+\left|\frac{x}{p}-\frac{y}{q}\right|=2$$


For purposes of plotting with a computer, the implicit equation isn't terribly convenient to handle, so I'll throw in a set of parametric Cartesian equations for free, based on the parametric equations of the Lamé curve:

$$\begin{align*}x&=p\left(|\cos\,t|\cos\,t+|\sin\,t|\sin\,t\right)\\y&=q\left(|\cos\,t|\cos\,t-|\sin\,t|\sin\,t\right)\end{align*}$$

Here's another one, based on a special case of the parametric equations given in this answer:

$$\begin{align*}x&=p\left(\cos\left(\frac{\pi}{2}\lfloor u\rfloor\right)-(2u-2\lfloor u\rfloor-1)\sin\left(\frac{\pi}{2}\lfloor u\rfloor\right)\right)\\y&=q\left(\sin\left(\frac{\pi}{2}\lfloor u\rfloor\right)+(2u-2\lfloor u\rfloor-1)\cos \left(\frac{\pi}{2}\lfloor u\rfloor\right)\right)\end{align*}$$

...and another one:

$$\begin{align*}x&=p\max\left(-1,\min\left(\frac4{\pi}\arcsin\left(\sin\left(\frac{\pi u}{2}+\frac{\pi}{4}\right)\right),1\right)\right)\\y&=q\max\left(-1,\min\left(-\frac4{\pi}\arcsin\left(\cos\left(\frac{\pi u}{2}+\frac{\pi}{4}\right)\right),1\right)\right)\end{align*}$$

...and I suppose I should stop here. ;)

Solution 2:

This is an equation for a rectangle which has corners at $(a,b)$ and $(c,d)$

$$(x-a)(x-c)(y-b)(y-d)=0$$

but it extends a little beyond the corners, so instead

$$\sqrt{(a-x)(x-c)}\sqrt{(b-y)(y-d)}=0$$

which would throw an error for square roots of negative numbers

Solution 3:

Try plotting $x^n + y^n = p^n$ where $p$ is the side length and $n$ is an even number. The larger $n$ is, the sharper the sides are.

Solution 4:

I found recently a new parametric form for a rectangle, that I did not know earlier: $$ \begin{align} x(u) &= \frac{1}{2}\cdot w\cdot \mathrm{sgn}(\cos(u)),\\ y(u) &= \frac{1}{2}\cdot h\cdot \mathrm{sgn}(\sin(u)),\quad (0 \leq u \leq 2\pi) \end{align} $$ where $w$ is the width of the rectangle and $h$ is its height.

I have used this in modelling parametric ruled surfaces, where it seems to be rather handy.

Solution 5:

Maybe you're looking for something like this: for $x\in(-1,2)$ plot $y=|x|$ and $y=3-|x-1|$.