How to plot with TikZ a function that takes a sphere and projects it onto 2D space

plottikz-pgf

I'd like to take a sphere, then apply a linear transformation from 3D (the space of the sphere) into 2D. I wish to plot the resulting linear transformation. Currently my code looks like this:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz, tikz-3dplot}
\usepackage{pgfplots}
\usepackage{pgfmath}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[
declare function={
lin1(\x,\y) = 1.8*cos(\x)*sin(\y)+1*sin(\x)*sin(\y)+cos(\y);
lin2(\x,\y) = 0.8*cos(\x)*sin(\y)+1.5*sin(\x)*sin(\y)+.8cos(\y);
}]
\draw[blue] plot[domain=0:360, domain y=0:180] ({lin1(\x,\y)},{lin2(\x,\y)});
\end{tikzpicture}
\end{figure}

\end{document}

But I get the error that domain y does not exist (undefined control sequence). How could I go around this problem? Or is there another (simple) approach to do what I want?

Best Answer

Your plot – as I understand it – maps two values to two other values. How should that look like? Besides: TikZ doesn't support a second domain. ( supports more.)

How should it connect the points? Should it first iterate over \x and then \y or the otherway around?

I've packed your “double plot” in a foreach loop that iterates over one variable while plotting over the other. This makes very different figures.

I've commented the “one path” plot, I'm only showing the one where I can use different colors for the different iterations.

Code

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
  smooth,
  declare function={
    lin1(\x,\y) = 1.8*cos(\x)*sin(\y)+1.0*sin(\x)*sin(\y)+    cos(\y);
    lin2(\x,\y) = 0.8*cos(\x)*sin(\y)+1.5*sin(\x)*sin(\y)+0.8*cos(\y);
  }]
% \draw[help lines] (-2.5,-4.1) grid (7.5,2.1);
\draw[help lines] ( 2.5,-4.1) grid (7.5,2.1);
% \draw[blue] (0,0) foreach \valX in {0,15,...,359} {
%   plot[variable=\valY, domain=0:180] ({lin1(\valX,\valY)},{lin2(\valX,\valY)})};
  
% \draw[yshift=+-2cm, blue] (0,0) foreach \valY in {0,10,...,180} {
%   plot[variable=\valX, domain=0:360] ({lin1(\valX,\valY)},{lin2(\valX,\valY)})};

\tikzset{xshift=+5cm}
\foreach \valX[evaluate={\col=\valX/3.6;}] in {0,15,...,359}
  \draw[blue!\col!red] plot[variable=\valY, domain=0:180] ({lin1(\valX,\valY)},{lin2(\valX,\valY)});

\tikzset{yshift=+-2cm}
\foreach \valY[evaluate={\col=\valY/1.8;}] in {0,10,...,180}
  \draw[blue!\col!red] plot[variable=\valX, domain=0:360] ({lin1(\valX,\valY)},{lin2(\valX,\valY)});
\end{tikzpicture}
\end{document}

Output

enter image description here