This note is for some auxilary information on some maps between the disc and square. A fair amount of the math is convered by Fong1 2 and the survey by Lambers3. Specifically this contains:

  • A pair of animated plots between the various maps
  • Show the Jacobian matrices and determinates and visualize with interactive plots
  • Reduced complexity radial stretch in both directions
  • Reduced complexity concentric disc to square
  • Show a (perhaps) new radial approximate area preserving map

I will use the convention that $ \left(x,~y \right)$ is a coordinate on the square and $ \left(u,~v \right)$ a coordinate on the disc.

Define a signum like function:

\[\text{sgn}\left(x\right) = \begin{cases} 1 & x \geq 0 \\[2ex] -1 & x < 0 \end{cases}\]



Area distortion


This shadertoy is intended to visualize how shapes and angles are transformed under the various maps. Clicking through to the site allows interactivity.



Point set


As an alternate visualization this plot take uniform points on the square and shows how the various square to disc mappings transform the point set.




Radial Stretching


Simplest square to disc map is to simply stretch (scale factor of $L_1$ over $L_2$ norm):

\[\left(u,~v \right) = \frac{\text{max}\left( \abs{x},~\abs{y} \right)}{\sqrt{x^2+y^2}}\left(x,~y\right)\]


Fong followed by Lambers provide an alternate formulation intended to be computationally friendly. Note that we can reformulate the scale factor $s$ applied to the input coordinate as the following pseudo-code:

float x2 = x*x;
float y2 = y*y;
float m  = x2 >= y2 ? x : y;
float s  = abs(m)*inversesqrt(x2+y2+epsilon);
// return s*(x,y)


where $\text{epsilon}$ is some sufficiently small constant4. Formulated in this manner only needs a single select/cmov like operation and no branching to handle all cases including degenerate.


A method to measure area distortion (growth/shrinkage) at infinitesimal around a point is to compute the Jacobian determinate5 of the function.

First we need the Jacobian, for $x^2 > y^2$:

\[\left(x^2+y^2\right)^{-\frac{3}{2}} \left[ \begin{array}{cc} x^3+2 y^2 x & -x^2 y \\ y^3 & x^3 \\ \end{array} \right]\]


and for $x^2 \leq y^2$

\[\left(x^2+y^2\right)^{-\frac{3}{2}} \left[ \begin{array}{cc} y^3 & x^3 \\ -x y^2 & y^3+2 x^2 y \\ \end{array} \right]\]


The determinates of these are respectively:

\[\frac{x^2}{x^2+y^2} \\ \frac{y^2}{x^2+y^2}\]


Combining these into a single expression gives:

\[\frac{ \max \left(x^2, y^2\right)}{x^2+y^2}\]


The area of the square is 4 and that of the disc is $\pi$, so the value where no area distortion occurs is $\frac{\pi}{4} \approx 0.785398$. Larger/smaller values are the local stretch/compressing respectively. The plot of the area distortion (determinate):


The disc to square transform is simply inverting the scale factor:

\[\left(x,~y \right) = \frac{\sqrt{u^2+v^2}}{\text{max}\left( \abs{u},~\abs{v} \right)}\left(u,~v\right)\]


and like above all cases can be handled with no branches and a single select.



Concentric


Peter Shirley and Kenneth Chiu in 19976 derived an area preserving map between square to disc.


The square to disc map as given in Shirley’s blog post7 (modifications noted by Dave Cline):

\[\left(u,~v \right) = \begin{cases} \left(x \cos\left( \frac{\pi}{4} \frac{y}{x} \right), x \sin\left( \frac{\pi}{4} \frac{y}{x} \right) \right) & \abs{x} \geq \abs{y} \\[2ex] \left(y \cos\left( \frac{\pi}{2} - \frac{\pi}{4} \frac{x}{y} \right), y \sin\left( \frac{\pi}{2} - \frac{\pi}{4} \frac{x}{y} \right) \right) & \abs{x} < \abs{y} \end{cases}\]


Phase shift and pulling out signs:

\[\left(u,~v \right) = \begin{cases} \left(x \cos\left( \frac{\pi}{4} \frac{y}{x} \right), x \sin\left( \frac{\pi}{4} \frac{y}{x} \right) \right) & \abs{x} \geq \abs{y} \\[2ex] \left(y \sin\left( \frac{\pi}{4} \frac{x}{y} \right), y \cos\left( \frac{\pi}{4} \frac{x}{y} \right) \right) & \abs{x} < \abs{y} \end{cases}\]


The more interesting form will be architecture/code specific. The second form trig inputs are on $\pm\frac{\pi}{4}$, so the cosine terms are always positive and removable without sign fixup and a narrow enough range for light weight approximation.


The Jacobian for $\abs{x} \geq \abs{y}$:

\[\left[ \begin{array}{cc} \cos \left( \frac{\pi y}{4 x} \right) + \frac{\pi y}{4 x} \sin \left(\frac{\pi y}{4 x}\right) & -\frac{\pi}{4} \sin \left(\frac{\pi y}{4 x}\right) \\ \sin \left(\frac{\pi y}{4 x}\right)-\frac{\pi y}{4 x} \cos \left(\frac{\pi y}{4 x}\right) & \frac{\pi}{4} \cos \left(\frac{\pi y}{4 x}\right) \\ \end{array} \right]\]


The Jacobian for $\abs{x} < \abs{y}$:

\[\left[ \begin{array}{cc} \frac{\pi}{4} \cos \left(\frac{\pi}{4 y} (x+y) \right) & \sin \left(\frac{\pi}{4 y} (x+y)\right)-\frac{\pi x}{4 y} \cos \left(\frac{\pi}{4 y}(x+y)\right)\\ -\frac{\pi}{4} \sin \left(\frac{\pi}{4 y} (x+y)\right) & \cos \left(\frac{\pi}{4 y}(x+y)\right)+\frac{\pi x}{4 y} \sin \left(\frac{\pi}{4 y}(x+y)\right) \\ \end{array} \right]\]


The determinant of both are constant $\left(\frac{\pi}{4} \right)$ as expected for an area preserving map.

The disc to square map:

\[\left(x,~y \right) = \begin{cases} \text{sgn}(u)\sqrt{u^2+v^2} \left(1, \frac{4}{\pi}\text{atan}\left(\frac{v}{u}\right) \right) & u^2 > v^2 \\[2ex] \text{sgn}(v)\sqrt{u^2+v^2} \left(\frac{4}{\pi}\text{atan}\left(\frac{u}{v}\right) , 1\right) & u^2 \leq v^2 \end{cases}\]


Where $\text{atan}$ is single parameter. This is degenerate when $v$ is approaching zero. This could be corrected by a select or since $\text{atan}$ is an odd function one possible rewrite choice is:

\[\left(x,~y \right) = \begin{cases} \sqrt{u^2+v^2} \left(\text{sgn}(u), \frac{4}{\pi}\text{atan}\left(\frac{v}{\abs{u}}\right) \right) & u^2 > v^2 \\[2ex] \sqrt{u^2+v^2} \left(\frac{4}{\pi}\text{atan}\left(\frac{u}{\abs{v}+\epsilon}\right) , \text{sgn}(v)\right) & u^2 \leq v^2 \end{cases}\]


In either case the range of $\text{atan}$ in on $\pm 1$ so can be lightweight (for it) approximated. ($\epsilon$ is some small constant4 as above)



F.G. Squircle


Fong1 derivated mappings based on Fernandez-Guasti’s squircle.

\[\left(u,~v \right) = \frac{\sqrt{x^2+y^2 - x^2 y^2}}{\sqrt{x^2+y^2}} \left( x,y \right)\]


The Jacobian:

\[\frac{1}{\left(x^2+y^2\right)^{3/2} \sqrt{y^2-x^2 \left(y^2-1\right)}} \left[ \begin{array}{cc} y^4-\left(y^2-1\right) x^4-2 y^2 \left(y^2-1\right) x^2 & -x^5 y \\ -x y^5 & y^4+\left(1-2 y^2\right) x^4-y^2 \left(y^2-2\right) x^2 \\ \end{array} \right]\]


The Jacobian determinate:

\[1-\frac{2 x^2 y^2}{x^2+y^2}\]


the plot of the area distortion (determinate):


The disc to square map:

\[n = u^2+v^2 \\ \\ s = \frac{\text{sgn}(uv)}{\sqrt{2}}\sqrt{n-\sqrt{n\left(n-4u^2v^2\right)}}\] \[\left(x,~y \right) = s \left(\frac{1}{u}, ~\frac{1}{v} \right)\]



Elliptical


The square to disc mapping was derived by Nowell in a blog post8:

\[\left(u,~v \right) = \left( x \sqrt{1-\frac{y^2}{2}} , ~y \sqrt{1-\frac{x^2}{2}} \right)\]


The Jacobian:

\[\left( \begin{array}{cc} \sqrt{1-\frac{y^2}{2}} & -\frac{x y}{\sqrt{4-2 y^2}} \\ -\frac{x y}{\sqrt{4-2 x^2}} & \sqrt{1-\frac{x^2}{2}} \\ \end{array} \right)\]


The Jacobian determinate:

\[\frac{2-\left(x^2+y^2\right)}{\sqrt{2-x^2} \sqrt{2-y^2}}\]


the plot of the area distortion (determinate):


Fong1 provides two derivations of the inverse map, one of which:

\[\left(x,~y \right) = \frac{1}{2}\left(\sqrt{2+t+2\sqrt{2}u}-\sqrt{2+t-2\sqrt{2}u} ,\sqrt{2-t+2\sqrt{2}v}-\sqrt{2-t-2\sqrt{2}v} \right)\]

where:

\[t = u^2-v^2\]



Approximate equal area


I haven’t been able to find this method referenced anywhere and a twitter query came up negative as well.

The basic idea here is to form a low complexity middle ground between radial stretch and concentric. See the point set animation above. As such it is only of interest if computationally cheaper than concentric and higher performance is a priority.


The disc to square map:

\[\left(x,~y \right) = \begin{cases} \left( \text{sgn}(u) \sqrt{u^2+v^2} , ~\sqrt{2}v \right) & u^2 > v^2 \\[2ex] \left( \sqrt{2}u , ~\text{sgn}(v) \sqrt{u^2+v^2} \right) & u^2 \le v^2 \end{cases}\]


The square to disc map:

\[\left(u,~v \right) = \begin{cases} \left( x \sqrt{1-\frac{y^2}{2x^2}} , ~\frac{y}{\sqrt{2}} \right) & x^2 > y^2 \\[2ex] \left( \frac{x}{\sqrt{2}} , ~y \sqrt{1-\frac{x^2}{2y^2}} \right) & x^2 \le y^2 \end{cases}\]


Which is degenerate when $y \le x $ and $y$ approaching zero. Since the division term is positive we can simply add a bias as before. More likely to be interesting is we can multiply through by the denominator and get:

\[\left(u,~v \right) = \begin{cases} \frac{1}{\sqrt{2}} \left(\text{sgn}(x)\sqrt{2x^2-y^2} , ~y \right) & x^2 > y^2 \\[2ex] \frac{1}{\sqrt{2}} \left(x , ~\text{sgn}(y)\sqrt{2y^2-x^2} \right) & x^2 \le y^2 \end{cases}\]


or leave the half inside:

\[\left(u,~v \right) = \begin{cases} \left(\text{sgn}(x)\sqrt{x^2-\frac{y^2}{2}} , ~\frac{y}{\sqrt{2}} \right) & x^2 > y^2 \\[2ex] \left(\frac{x}{\sqrt{2}} , ~\text{sgn}(y)\sqrt{y^2-\frac{x^2}{2}} \right) & x^2 \le y^2 \end{cases}\]


The Jacobian for $\abs{x} \geq \abs{y}$:

\[\left( \begin{array}{cc} \frac{1}{\sqrt{1-\frac{y^2}{2 x^2}}} & -\frac{y}{x \sqrt{4-\frac{2 y^2}{x^2}}} \\ 0 & \frac{1}{\sqrt{2}} \\ \end{array} \right)\]


and the determinate:

\[\frac{1}{\sqrt{2-\frac{y^2}{x^2}}}\]


The Jacobian for $\abs{x} < \abs{y}$:

\[\left( \begin{array}{cc} \frac{1}{\sqrt{2}} & 0 \\ -\frac{x}{\sqrt{4-\frac{2 x^2}{y^2}} y} & \frac{1}{\sqrt{1-\frac{x^2}{2 y^2}}} \\ \end{array} \right)\]


and the determinate:

\[\frac{1}{\sqrt{2-\frac{x^2}{y^2}}}\]


and the heat map of area distortion. Recall equal area value is $\frac{\pi}{4} \approx 0.785398$



Approximate area preserving (variant 2)


In another post9 we have a volume preserving map between cylinder and sphere which breaks the space into two parts: point inside and outside of the embedded conic. We can take a planar slice through the cylinder’s axis to form a square to disc map. Reducing the 3D equation to the slice gives:

\[\left(u,~v \right) = \begin{cases} \left(x \sqrt{1-\frac{4}{9}\frac{y^2}{x^2}},~\frac{2}{3}y \right) & x^2 \geq y^2 \\[2ex] \left(x \sqrt{\frac{2}{3}-\frac{1}{9}\frac{x^2}{y^2}},~ y-\frac{1}{3}\frac{x^2}{y}\right) & x^2 < y^2 \end{cases}\]


We can rewrite the $x^2 \geq y^2$ case to eliminate division (which can be zero) as:

\[t = \frac{2}{3}y \\ \left( \text{sgn}\left(x\right) \sqrt{x^2-t^2}, ~t\right)\]


and re-express the $x^2<y^2$ case as:

\[t = \frac{x}{3y} \\ \left( x\sqrt{\frac{2}{3}-t^2}, ~y-xt\right)\]


The Jacobian for for the cases are:

\[\left( \begin{array}{cc} \frac{3}{\sqrt{9-\frac{4 y^2}{x^2}}} & -\frac{4y}{3 x \sqrt{9-\frac{4 y^2}{x^2}}} \\ 0 & \frac{2}{3} \\ \end{array} \right)\] \[\left( \begin{array}{cc} \frac{6y^2-2x^2}{3\sqrt{6-y^2\frac{x^2}{y^2}}} & \frac{x^3}{3y^3\sqrt{6-\frac{x^2}{y^2}}} \\ -\frac{2x}{3y} & 1+\frac{x^2}{3y^2} \\ \end{array} \right)\]


with the determinates:

\[\frac{2}{\sqrt{9-\frac{4y^2}{x^2}}} \\ \frac{2}{\sqrt{6-\frac{x^2}{y^2}}}\]


The area distortion:


For the disc to square map, start with a common term:

\[t = \sqrt{u^2+v^2}\]


then the map is:

\[\left(x,~y \right) = \begin{cases} \left(u \sqrt{\frac{3t}{t+\abs{v}}}, ~\text{sgn}\left(v\right)t \right) & \frac{5}{4}v^2 \geq u^2 \\[2ex] \left(\text{sgn}\left(u\right)t,~ \frac{3}{2}v\right) & \frac{5}{4}v^2 < u^2 \end{cases}\]



References and Footnotes

  1. “Analytical Methods for Squaring the Disc”, Chamberlain Fong, 2015. (PDF 2 3

  2. “Analytical Methods for Squaring the Disc (presentation slides)”, Chamberlain Fong, 2014. (link

  3. “Mappings between Sphere, Disc, and Square”, Martin Lambers, 2016. (link

  4. A reasonable(ish) epsilon for the funcs here under generic usage is the smallest normal fp value (single: $2^{-126}$).  2

  5. “Jacobian matrix and determinant”, Wikipedia. (link

  6. “A Low Distortion Map Between Disk and Square”, Peter Shirley, Kenneth Chiu, 1997. (PDF

  7. “Improved code for concentric map”, Peter Shirley’s blog, 2011. (link

  8. “Mapping a Square to a Circle”, Philip Nowell, 2005. (link

  9. Cube/cylinder/ball mappings, (post



Comments





© 2023 Marc B. Reynolds
all original content is public domain under UNLICENSE