# Equations for this parameterised affine transform on the plane?

I'm trying to find a parameterised affine transform that will transform the unit circle into the family of ellipses shown.

The unit circle passes through (0,1) and (1,0) and is tangential to the lines (1,x) and (x,1). In this question, the circle is transformed into an ellipse that retains those properties. Because the properties only fix 4 parameters and a unique ellipse requires 5, there will be a family of ellipses, defined by a single parameter "p" ("p" being arbitrary within some range), which all share these same 4 properties with the unit circle.

Since a transform from a unit circle to an ellipse (however rotated or translated) is affine, I'd expect to be able to represent the transform from the unit circle to a member of this family of ellipses in this kind of form:

x -> A.x + B.y + C
y -> D.x + E.y + F


where A,B,C,D,E,F are functions of "p".

(It isn't important what "p" actually represents, so long as it parameterises the family of ellipses.)

I'm having difficulty getting a suitable transform for this.

The approach I tried was to look at what happens to 3 easy points under the required transform: (0,1) and (1,0) stay fixed at (0,1) and (1,0), and (0,0) must be mapped due to symmetry, to some other point on the diagonal (Z, Z) where Z < 1/sqrt(2) and Z will often be negative. This gives 6 trivial equations in 6 variables, and as there should be just one solution for any given value of the parameter this should be it.

But when I do this, I get an ellipse that isn't tangential to the 2 lines, I think that's because the lines that the ellipse I get is tangent to at the fixed points, have also been transformed and are no longer horizontal/vertical.

Has my approach inadvertently loosened up the parameters so that I'm getting multiple families? If not, then what's going on, and what do I have to do to fix it?

Your assumption that the two tangent points of the circle will remain fixed is leading you astray, as you’ve discovered. As MvG explains in his answer, the only affine transformation that leaves these points fixed and maps the lines $x=1$ and $y=1$ to themselves is the identity. Instead, we’ll build up the transformation in stages. It will consist of a dilation, a 45° rotation and a translation.

To make the calculations simpler, consider an ellipse in standard position. We want its normal at the point in the first quadrant with $y=1/\sqrt2$ to be parallel to the line $x=y$. From this condition, we get $${x\over a^2}=\frac1{\sqrt2b^2}\quad\text{and}\quad{x^2\over a^2}+{1\over2b^2}=1.$$ Combining these two equations gives a relationship between the major and minor axes of the ellipse: $$a^2=b^2(2b^2-1).$$ Solving for $b$ looks a bit messy, so we’ll take it (the semi-minor axis length) as our parameter. (Note that we must have $b\gt1/\sqrt2$ for an ellipse.) We thus get the dilation matrix $$S=\begin{bmatrix}b\sqrt{2b^2-1}&0&0\\0&b&0\\0&0&1\end{bmatrix}.$$ Next, we compute the required translation. For our point of interest to end up at $(0,1)$ after rotation, we must translate it to $(1/\sqrt2,1/\sqrt2)$, which can be accomplished by translating by $$\frac1{\sqrt2}-\frac1{\sqrt2}{a^2\over b^2} = \sqrt2(1-b^2)$$ along the $x$-axis. Alternatively, we can translate last, in which case the translation will be along the line $x=y$, giving the translation matrix $$T=\begin{bmatrix}1&0&1-b^2\\0&1&1-b^2\\0&0&1\end{bmatrix}.$$
The rotation matrix is, of course, $$R=\begin{bmatrix}\frac1{\sqrt2}&-\frac1{\sqrt2}&0\\\frac1{\sqrt2}&\frac1{\sqrt2}&0\\0&0&1\end{bmatrix}.$$ Composing these three transformations produces the required family of affine transformations: $$M=TRS=\begin{bmatrix}b\sqrt{b^2-\frac12}&-\frac1{\sqrt2}b&1-b^2\\b\sqrt{b^2-\frac12}&\frac1{\sqrt2}b&1-b^2\\0&0&1\end{bmatrix}.$$ If we take $p=2b^2-1$, this becomes $$\begin{bmatrix}-\frac12\sqrt{p(1+p)}&-\frac12\sqrt{1+p}&\frac12(1-p) \\ -\frac12\sqrt{p(1+p)}&\frac12\sqrt{1+p}&\frac12(1-p) \\ 0&0&1\end{bmatrix}$$ for $p>0$, which is a perhaps more aesthetically pleasing range.

The equation for this family of ellipses is $$\begin{bmatrix}x&y&1\end{bmatrix}(M^{-1})^T\begin{bmatrix}1&0&0\\0&1&0\\0&0&-1\end{bmatrix}M^{-1}\begin{bmatrix}x\\y\\1\end{bmatrix}=0.$$ $M^{-1}$ is easily computed by inverting its components individually, resulting in $$\begin{bmatrix}x&y&1\end{bmatrix}\begin{bmatrix}\frac1p&-{p-1\over p(p+1)}&{p-1\over p(p+1)} \\ -{p-1\over p(p+1)}&\frac1p&{p-1\over p(p+1)} \\ {p-1\over p(p+1)}&{p-1\over p(p+1)}&-{3p-1\over p(p+1)}\end{bmatrix}\begin{bmatrix}x\\y\\1\end{bmatrix}=0$$ or, after multiplying through by $p(p+1)$, $$(p+1)x^2-2(p-1)xy+(p+1)y^2+2(p-1)(x+y)-3p+1=0.$$ Alternatively, we can multiply the $M^{-1}\begin{bmatrix}x&y&1\end{bmatrix}^T$ terms out first, yielding $${(y-x)^2\over p+1}+{(x+y+p-1)^2\over p(p+1)}=1.$$ The parameter $p$ turns out to have an interesting meaning: the resulting ellipse will be tangent to the lines $x=-p$ and $y=-p$ at $(-p,1-p)$ and $(1-p,-p)$, respectively, i.e., it gives the other two sides of the bounding box of the ellipse.

• All the above were really helpful and high quality, its very hard to pick just one. But you broke it down really clearly and spot on for the question I had. Thank you very much, its very clear now. – Stilez Oct 8 '16 at 11:31
• @Stilez I’ve encountered these ellipses before, but generating them via a family of transformation was a novel and interesting approach for me. – amd Oct 8 '16 at 17:57
• Yes! I just woke up to the power of using transforms myself, which led me here. Rather than solving the equations for the ellipses in this family, I started with solving for the unit circle (trivial) and transform that to the ellipse concerned; as long as I can parameterise the transform for an arbitrary member of the family of ellipses this works out easier for the problem I'm solving, as it gives me almost the next best thing to an analytic solution, namely an easily calculable one. But I missed that points can shift round it. Glad it gave you an interesting question - it did to me as well! – Stilez Oct 8 '16 at 19:58
• The transformation matrix M = TRS is the key for me, as I'm after the transform rather than equation here. Also I found your first step easier this way - the normal at (X,Y) on the ellipse 'x^2/a^2 + y^2/b^2 = 1 has the equation a^2.Y.(x-X) = b^2.X.(y-Y), or in standard form, y= ((a^2.Y/b^2.X)).x + ((1-a^2/b^2).Y). For the normal in the first quadrant to be at 45 degrees, the first term = 1 so the point must obey Y = (b^2/a^2).X. It comes to the same thing but thought this would help. On an aside, the choice of p` is practically easier as well as aesthetic – Stilez Oct 8 '16 at 20:46
• @Stilez I just took the gradient to get a normal vector and set its components equal to each other. – amd Oct 8 '16 at 20:50

Rotate the figure by $45°$ to align on the coordinate axis and dilate by $\sqrt2$ for convenience.

The ellipse equation turns to

$$a^2(x-p)^2+b^2y^2=1$$ where $a^2,b^2$ and the inverse of the squared axes lengths and $p$ the position of vertical axis.

The slopes are given by

$$a^2(x-p)+byy'=0.$$

Express that the ellipse passes through $(1,\pm1)$ with slope $\mp1$:

$$a^2(1-p)^2+b^2=1,$$ $$a^2(1-p)-b^2=0.$$ (By symmetry, the other two equations are identical.)

You can solve the $2\times2$ linear system for $a^2$ and $b^2$ as functions of $p$, and you obtain the affine transform

$$(x,y)\to(a(p)(x-p),b(p)y).$$

Compose with the reverse scaling by $\sqrt2$ and the counter-rotation by $45°$ to get the final answer.

Something like $$(x,y)\to\left(\frac{(a(p)(x-p)-b(p)y}2,\frac{a(p)(x-p)+b(p)y}2\right).$$

• All the above were really helpful and high quality, its very hard to pick just one. This was a very clear cogent response, and provided a direct way to solve the underlying problem. – Stilez Oct 8 '16 at 11:33
• @Stilez: yep, I prefer concise solutions. – Yves Daoust Oct 8 '16 at 20:08
• Thanks for making me aware I had a mistake in my answer, and for poroviding a formula from which I could compute example values in order to debug my computation. Seems I misread the role of the extra degrees of freedom I observed at some point. – MvG Oct 9 '16 at 21:03

Although your transformations should preserve the tangency conditions, this does not mean that the points of tangency are fixed points. In fact they can not be. Think about it this way: if you have two points and the tangent directions in these points, then these uniquely define the point where the tangents intersect. If all of this is to remain fixed, you have three fixed points and the only affine transformation with three non-collinear fixed points is the identity. Ergo your points of tangency can't be fixed.

## Family of conics

So what's your family of conics? You already know one such conic, namely the unit circle:

$$x^2+y^2-1=0$$

Another one is the degenerate ellipse of zero minor semiaxis which passes through the two points of tangency:

$$(x+y-1)^2=x^2+y^2-2x-2y+2xy+1=0$$

The family of conics (or pencil of conics) would be the set of all linear combinations of these two. Introducing a parameter $p$ you could write them as

$$(x^2+y^2-1)+2p(x+y-xy-1)=0$$

or something like that.

## Describing desired transformations

In order to find your affine transformation, start by writing this as a matrix:

$$(x,y,1)\cdot\begin{pmatrix} 1 & -p & p \\ -p & 1 & p \\ p & p & -1-2p \end{pmatrix}\cdot\begin{pmatrix} x\\y\\1\end{pmatrix}=0$$

Applying an affine transformation to a conic means conjugating the matrix of the conic with the matrix of the inverse transformation. If we want to apply the forward transformation instead, we can say that this forward transformation should turn the conic for parameter $p$ back into the unit circle:

$$\begin{pmatrix} A & D & 0 \\ B & E & 0 \\ C & F & 1 \end{pmatrix}\cdot\begin{pmatrix} 1 & -p & p \\ -p & 1 & p \\ p & p & -1-2p \end{pmatrix}\cdot\begin{pmatrix} A & B & C \\ D & E & F \\ 0 & 0 & 1 \end{pmatrix}=q\cdot\begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & -1 \end{pmatrix}$$

The variable $q$ on the right hand side is required because any multiple of that matrix will describe the same conic (so the matrix is homogeneous). Any non-zero value for $q$ is acceptable.

## The space of all solutions

This matrix equation gives you nine scalar equations, but due to the matrix being symmetric, only six of them are actually distinct. And even among those there are interdependencies, so that for a given $p$ the set of solutions forms a two-dimensional variety. Roughly speaking you have two real degrees of freedom still to choose your transformation.

## Specific solutions

To obtain a unique solution, you may want to require $A=E,B=D,C=F$, i.e. make your transformation fully symmetric with respect to swapping the $x$ and $y$ coordinates. With the above approach you end up with four quadratic equations for four variables $A,B,C,q$:

\begin{align*} A^2p + B^2p - 2AB &= 0 \\ A^2 + B^2 - 2ABp &= q \\ 2C^2p - 2C^2 - 4Cp + 2p + 1 &= q \\ (A+B)(C - Cp + p) &= 0 \\ \end{align*}

But depending on your application, other transformations might be of interest as well, e.g. this one here (for $p=-\frac12$ and with additional conditions $2A=E,C=F$):

$$\begin{pmatrix} A & B & C \\ D & E & F \\ 0 & 0 & 1 \end{pmatrix}=\begin{pmatrix} \sqrt{\frac{5-2\sqrt3}{13}} & \sqrt{\frac{47+2\sqrt3}{13}} & -1 \\ 2\sqrt{\frac{8+2\sqrt3}{13}} & 2\sqrt{\frac{5-2\sqrt3}{13}} & -1 \\ 0 & 0 & 1 \end{pmatrix}$$

## Non-ellipse solutions

Note that you can't expect to always find such a matrix for any $p$ you may choose. For example, for $p=1$ you get a parabola. So your family of conics contains parabolas and hyperbolas. You can't get these from affine transformations of a circle.

In my first versions of this post I had forgotten about the parameter $q$ above, leading me to conclude that no such transformation could exist. In order to offer some alternatives, I then discussed ways to use projective transformations instead. If you need those other conics as well, please look at the revision history of this post for details on that aspect.

I also tried out Yves' answer for $p=-1$ in his formulas which corresponds to $p=\frac13$ in mine. I came up with $a=-\sqrt{1/6}, b=-\sqrt{1/3}$ and
$$\begin{pmatrix} A & B & C \\ D & E & F \\ 0 & 0 & 1 \end{pmatrix}=\begin{pmatrix} -\sqrt{\frac32} & \frac{\sqrt3}2 & -\frac12 \\ -\sqrt{\frac32} & -\frac{\sqrt3}2 & -\frac12 \\ 0 & 0 & 1 \end{pmatrix}$$
which looks more like $A=D,B=-E,C=F$ as additional constraint. I'm a bit surprised by this at the moment, but I might have made a mistake somewhere.