# 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.

-
Do you mean the like of $0 \le x \le 1 \land 0 \le y \le 1$ ? Or do you need to parametrize the boundary of the rectangle ? –  Sasha Oct 1 '11 at 21:05
I'm looking for a cartesian equation of a rectangle. For example the equation of a circle is $x^2 + y^2=a^2$ –  Cobold Oct 1 '11 at 21:11
$max(|x|,|y|)=1$ is the implicit equation for the 'unit rectangle' (it is in fact the 'unit sphere/circle' under the inf/maximum-norm) wolfram alpha plot –  Peter Sheldrick Oct 1 '11 at 22:03
An implicit Cartesian equation would be the one Peter gave. Methinks that ain't much. Maybe you want a parametric equation? –  Ｊ. Ｍ. Oct 1 '11 at 22:28

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. ;)

-
i upvoted the answer and i think Cobold should accept it (parametric equations are great for the computer!). However, the statement 'For purposes of plotting with a computer, the implicit equation isn't of much use' isn't okay like that. If it is so impossible to plot implicit equations - how did wolfram alpha do it in the link in posted in my comment? Also implicit equations are usually succint and are easy to work with algebraically. Think of linear equations for example. Or in this case with max() you can still do boolean operations with sets described with the equations. –  Peter Sheldrick Oct 1 '11 at 23:29
I did say "for plotting purposes", @Peter. There are things like GrafEq and Mathematica for plotting implicit Cartesian equations, but in general they require way much more effort on the part of the computer to plot than parametric equations (have you seen the algorithms behind implicit equation plotters?). –  Ｊ. Ｍ. Oct 1 '11 at 23:32
I'm informing myself a bit because i love being able to do implicit plotting. There are special cases of course for example if the curve is differentiable and you have a point on the curve, then you can trace out the curve because it will be perpendicular to the gradient (and other special cases). But i know that in general there are many ways to do implicit plotting, and all of them are very involved. If someone here is feeling bored you might implement implicit plotting for the GeoGebra guys, because apparently they aren't happy with what they have. geogebra.org/trac/wiki/Implicit –  Peter Sheldrick Oct 1 '11 at 23:43
in geogebra.org/trac/wiki/Implicit/Software they mention another three plotters that can also handle implicit equations, namely GCalc, GnuPlot and Grapher2D (with source-code) –  Peter Sheldrick Oct 2 '11 at 0:06
I posted a new question based on this one. I want the equation of a square where each point is at the same angle as the input angle $t$, which is not true of the parametric equation above. –  Zev Eisenberg Jan 29 at 1:53

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

-

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

-

In general, the implicit formula for a rectangle a la $x^2 + y^2 = a^2$ for circles is not going to be well defined. This should be at least somewhat clear, as the boundary of a rectangle is not analytic (smooth) like the boundary of a circle is. I suppose we could generate a piece wise function to graph the edges, something like: $$f(x,y) = \begin {cases} (x,b) , (x,0) & 0 \leq x \leq b \\ (0,y) , (a,y) & 0 \leq y \leq a \end {cases}$$ For a rectangle with its bottom left corner at (0,0) and sides a,b. Such a function is messy, still non-analytic and doesn't help you that much. Ultimately, I think searching for a good implicit function of a rectangle is going to be nonproductive. What problem are you trying to apply this to? Any comment as to your next steps / applications for the equation you're searching for will prove helpful.

-

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.

-

As i wrote the implicit equation for a square is just $$\max(|x|,|y|)=1$$ - easily modifiable to get a translated rectangle etc.

When i wrote this comment i was a bit confused how to plot such an implicit function unfortunately - i just relied on whatever implementation was supplied to me (for example matlab or wolfram alpha etc).

However the marching squares algorithm is one candidate of such an implicit function plotter. In 3D that generalizes to the marching cubes algorithm.

-

If the equations of the diagonals of the rectangle are Ax + By + C = 0 and Dx + Ey + F = 0 then an equation for the rectangle is:

M|Ax + By + C| + N|Dx + Ey + F| = 1

M and N can be found by substituting the coordinates of two adjacent vertices of the rectangle.

In fact, this equation can be used to describe any parallelogram. Roughly speaking, M (together with A and B) and N (together with D and E) give the size of the diagonals of the parallelogram.

-