Home An interactive visualisation of immersed surfaces on Desmos
Post
Cancel

An interactive visualisation of immersed surfaces on Desmos

Note: this article is currently a WIP.

Some examples of surface visualisations

Here are a couple of versions of the Desmos save file with different cool pre-loaded plots:

Note: The below was submitted as my essay for the final assignment in the unit MTH3110 - differential geometry, at Monash University, in Semester 1, 2021. I’ve chosen to upload it here for those who may have seen my Desmos surface visualisation, and are interested in how I derived it! However, it assumes familiarity, especially near the end, with differential-geometric quantities such as maps of surfaces and their derivatives, and tangent spaces.

The explanation and geometry of the Desmos visualisation

Here is an interactive visualisation of surfaces on Desmos (a graphing website), made by me. It takes a (continuous) function f:UR, for some UR2, and plots a projection of the graph of f onto R2 as a “wire-frame”; it can also take a parametrisation σ:UR3 and plot a projection of its image in the plane.1 We discuss the algebra and geometry used in its construction, and some geometric properties of the projection map. Henceforth, let us assume that U is open, f is a smooth function, and that σ is a regular surface patch.

Projecting down to a plane

There are many ways to project R3, onto R2. However, a way to choose a sensible, well-behaved projection is to choose a plane Π through the origin (a linear subspace of R3), and perform an orthogonal projection π. Such a plane can be uniquely determined by using spherical coordinates. Consider an “observer” at NS2, the unit sphere, and a standard map τ:R2S2 given by

τ(u,v)=(cosucosv,cosusinv,sinu);

here, (u,v) respectively measure latitude and longitude.2 The unit vectors in T0R3 (denoting possible directions from the origin) are precisely points on S2. Thus, we may take Π as the orthogonal complement of the span of {N}.

A fact from differential geometry is that TNS2={vR3:Nv=0}. If vΠ, then for any kNspan{N} (where kR), (kN)v=k(Nv)=0. In particular, Nv=0, so vTNS2. Conversely, a vector vTNS2 satisfies (kN)v=k(Nv)=0 for all kR, so vΠ. This means that Π=TNS2, and N is normal to this tangent space.

Next, we consider the problem of projecting from R3 into Π (and then into R2). Given an orthonormal basis b={b1,b2} for Π that extends to a right-handed orthonormal basis b={b1,b2,N} of R3, where N=b1×b2, consider the orthogonal projection π:R3Π. Since kerπ=Π and π|Π is the identity, it follows that π(b1)=b1, π(b2)=b2, and π(N)=0. The map π is linear, so its matrix with respect to the bases b,b is

πb,b=(100010).

The geometry of this map is as follows: take a point pR3. Then the coset p+Π={p+kN:kR}, a line parallel to N (thus orthogonal to Π) passing through p, is collapsed onto a point π(p)Π; distinct parallel lines are collapsed to distinct points. This is an orthographic projection, where the axes are not foreshortened, that is, no length distortion due to perspective (see Figure~\mathbb Ref{fig:cylinder_perspective}).\footnote{See explanation of perspective and foreshortening in art.} Under this projection, we consider R3 as the quotient R3/Π, and cannot distinguish between points on the same line parallel to Π; the map has rank 2 and nullity 1.

\begin{figure}[p!] \centering \includegraphics[width=0.75\textwidth]{cylinderperspective.png} \caption{Plot of cylinder σ:(0,2π)×(4,4)S, σ(u,v)=(v,cosu,sinv) with perspective (θ,ϕ)=(0.1,0.1), using the Desmos visualisation. Observe that there is no distortion due to perspective (i.e. _foreshortening) in the negative x-direction.} \label{fig:cylinder_perspective} \end{figure}

Next, we consider the map that transforms points on Π into points in R2. Given our basis b for Π, there is a natural sense in which b1 can be thought of as pointing “right”, and b2 as pointing “up”. This is described by T:ΠR2, the linear isometry such that T(b1)=e1 and T(b2)=e2. Letting e={e1,e2}R2, T is simply the identity in coordinates b,e: Tb,e=I2, the 2×2 identity matrix.

Thus, our overall projection π:R3R2 in the direction of Π would be given by π=Tπ, with matrix Tb,eπb,b. However, we wish to express the matrix for π with respect to the standard basis e={e1,e2,e3} for R3. For this, we recall that the change-of-basis matrix from b to e is given by

Mbe=(|||b1b2N|||).

Since b is an orthonormal set, it follows that MbeO(3), so Meb=(Mbe)1=(Mbe). It follows that the matrix for π w.r.t. the bases e,e of R3,R2 respectively is

πe,e=Tb,eπb,bMeb=(1001)(100010)(---b1------b2------N---)=(---b1------b2---)=:M.

Now, recall that there is a pair (θ,ϕ) corresponding to N=τ(θ,ϕ). For simplicity, we will assume that θ(π/2,π/2), so that a suitable restriction of τ forms a regular chart for S2. We aim to find an orthonormal basis for Π by finding an orthonormal basis for TNS2. Note that {τu,τv} forms a basis for TNS2, where

τu(θ,ϕ)=(sinθcosϕ,sinθsinϕ,cosθ)

and

τv(θ,ϕ)=(cosθsinϕ,cosθcosϕ,0).

It is easy to check that these are orthogonal, so an orthonormal basis {b1,b2} for Π is found by normalising these vectors, yielding

b2=(sinθcosϕ,sinθsinϕ,cosθ)

and

b1=(sinϕ,cosϕ,0).

(It turns out that this also works when θ{±π/2}; these span the tangent planes T(0,0,±1)S2=R2×{0}!) Note that we needed to take b1=τv/τv and b2=τu/τu, so that indeed b1×b2=N=τ(θ,ϕ).

In summary, for our perspective given by spherical coordinates (θ,ϕ) (or equivalently N=τ(θ,ϕ)S2), the projection π:R3R2 is given by

(*)π(x,y,z)=(sinϕcosϕ0sinθcosϕsinθsinϕcosθ)M=M(θ,ϕ)(xyz)=(xsinϕ+ycosϕxsinθcosϕysinθsinϕ+zcosθ).

Visualising the surface in Desmos

If we wish to plot S as the graph of f, take σ(u,v)=(u,v,f(u,v)). Then it reduces to the problem of plotting the image of σ, or at least its projection under π. It would be useless (given limitations on Desmos) to present a filled-in outline—we would get a single splotch of colour (with jagged edges)! Instead, we use the idea that the domain U is an open subset of R2,\footnote{Desmos does not like strict inequalities, so they are often closed subsets instead, but we can ignore the boundary of U.} and for fixed (u,v)U, we consider the curves xu,yv:Iu,JvU in U (for suitable open intervals Iu,Jv), where xu(t)=(u,t) and yv(t)=(t,v), i.e. we fix the u or v coordinate, and let the other vary.

Consider a set of points {(ui,vi)}iU, and the families of curves {xui}i,{yvi}i so-defined. Then for each of the curves (generically called α:IU), we plot the image of γ=σα:IS, a curve in S, under the projection π, yielding two families {πσxui}i (fixing (x=)u=ui, plotted in red) and {πσyvi}i (fixing (y=)v=vi, plotted in blue) of curves in R2, representing moving along S using σ, with one input fixed. In the visualisation, we assume that U=(a1,a2)×(b1,b2) is a box,\footnote{Technically U=[a1,a2]×[b1,b2], so we have a surface with boundary… but we can ignore this, as mentioned earlier.} and take n1,n2 curves of constant separation in U in each direction. This creates the desired projection of the “wire-frame” on S, and varying (θ,ϕ) allows us to visualise the plot of S from all angles, giving it a 3-dimensional effect.

Differential geometry of the projection: changing perspective

For spherical coordinates (θ,ϕ), let principal latitude denote θ0[π/2,π/2] such that there is ϕ0R with τ(θ,ϕ)=τ(θ0,ϕ0). We see that M(θ,ϕ) is a smooth function of θ,ϕ, so the matrix (and projection) smoothly varies with θ,ϕ:

Mθ(θ,ϕ)=(000cosθcosϕcosθsinϕsinθ)

and

(from (*)).Mϕ(θ,ϕ)=(cosϕsinϕ0sinθsinϕsinθcosϕ0)

Fixing a point p=(x,y,z)R3, we can think of π as a function R2R2, where the inputs are spherical coordinates (θ,ϕ); let us call this map π:R2R2, (θ,ϕ)π(p)=M(θ,ϕ)p. Then the directional derivatives of π are given by D(θ,ϕ)π:T(θ,ϕ)R2Tπ(θ,ϕ)R2, which is such that

(λ,μ)(||πθπϕ||)Dπ(θ,ϕ)(λμ)=(0xcosϕysinϕxcosθcosϕycosθsinϕzsinθxsinθsinϕysinθcosϕ)(λμ).

So, if we change perspective from (θ,ϕ) with velocity (λ,μ)T(θ,ϕ)R2 (i.e. increasing latitude by λ and longitude by μ), the image of p in R2 moves with velocity D(θ,ϕ)π(λ,μ), given explicitly above. Note that

D(θ,ϕ)π(λ,μ)=λMθ(θ,ϕ)p+μMϕ(θ,ϕ)p;

this relates the directional derivatives of π to the partial derivatives of M.

Let us consider what happens to π(p) when we fix longitude ϕ and vary latitude θ. Taking λ=1 and μ=0 reveals a directional derivative of

Mθ(θ,ϕ)p=(0,xcosθcosϕycosθsinϕzsinθ)

in the (1,0) direction. In particular, the x-coordinate of π(p) does not change when only θ is changed, which makes sense since we view this as tilting the plane Π up or down. However, the y-coordinate changes at a rate precisely equal to τ(θ,ϕ)p=Np=τ(θ,ϕ+π)p (since τ gives points on S2). For nonzero p, this is zero precisely when p, or equivalently p/pS2, is orthogonal to NS2, the unit normal vector to the plane of projection Π. This holds when NTp/pS2; if τ(R×{ϕ})Tp/pS2, then this occurs for every latitude θ, but otherwise, τ(R×{ϕ}) is a circle on S2 and intersects Tp/pS2 for precisely one latitude θ (modulo π). From this, we conclude that for principal latitudes θ, if p/p=±(N×τ(θ+π/4,ϕ)), π(p) is always fixed when varying latitude θ (the directional derivative is zero); otherwise, there is precisely one latitude θ (or two, whence θ=±π/2) for which π(p) has velocity zero.

The above observations agree with a direct calculation: the curve in R2 that π(p) traces for fixed longitude ϕ is

βp:RR2,βp(t)=(xsinϕ+ycosϕconstant,xsintcosϕysintsinϕ+zcosttrigonometric with period 2π).

Now, let us consider what happens to π(p) when we fix latitude θ and vary longitude ϕ. Taking λ=0 and μ=1 gives a directional derivative of

Mϕ(θ,ϕ)p=(xcosϕysinϕ,xsinθsinϕysinθcosϕ)

in the (0,1) direction. A direct calculation gives that the curve in R2 that π(p) traces for fixed latitude θ is

δp:RR2,δp(t)=(xsint+ycost,xsinθcostysinθsint+zcosθ);

the above directional derivative is simply δ˙p(ϕ). By trigonometry, we know that letting T=t+cos1(y/x2+y2),

δp(t)=(x2+y2cosT,sinθx2+y2sinT+zcosθ);

this is clearly an ellipse (with period 2π) with axis lengths x2+y2 and |sinθ|x2+y2, oriented negatively (clockwise) whenever principal latitude is positive (and positively if principal latitude is negative). Additionally, it has 4 vertices by Example 6.3 of chapter 4 (as long as |sinθ|1 and both axis lengths are nonzero); these vertices correspond to locations where the x or y-coordinate of π(p) is greatest or smallest. In summary, we see that for fixed latitude θ, projected points move in an elliptic shape (in a clockwise direction when viewed from with positive principal latitude); this agrees with the intuition that as we increase longitude ϕ, in order for the normal N to the plane of projection Π to point “into the screen”, points must rotate clockwise about the projected z-axis.

This concludes our analysis of the 2-dimensional visualisation of surfaces in R3 on Desmos. We saw some commentary on the derivation of the projection, using some linear algebra. Then we proceeded to analyse how curves are represented in the plot, as a “wire-frame”, and finally, we analysed the effect of changing latitude and longitude on the positions of points in the plot, rounding off a treatment of the algebraic, analytic, and geometric properties of the visualisation.

Acknowledgements

I would like to acknowledge Dan Mathews for giving useful feedback (especially with respect to interpreting the effect on changing perspective on the position of points) and verifying the procedure used to derive the projection matrix. Errors in my initial formula for the projection were also corrected through discussion and testing with various members of the Maths @ Monash Discord server.

  1. To switch modes, click on the “plot” folder to hide it, and click on the “parametric plot” folder to show that. 

  2. See Example 2.17 from MTH3110 chapter 5 notes. 

This post is licensed under CC BY 4.0 by the author.