[Tex/LaTex] How to draw a curved surface in a polar coordinate system

3dpdftexpolarplottikz-3dplottikz-pgf

I'm fairly new to using TikZ and so far I've got a spherical coordinate system, and labelled the point P at which I want to draw a curved surface. A full sphere of unit radius, for example, would be a 'curved surface' of radius 1, with a polar angular width, del_theta, of 180 degrees and an azimuthal angular width, del_phi, of 360 degrees.

The curved surface is to be drawn at point P, (xp, yp, zp), with del_theta of say 10 degrees and del_phi of say 20 degrees. In summary, I want to express where this curved section is in Cartesian coordinates, and how big it is in terms of polar coordinates.

If not del_theta, and del_phi, I could instead provide theta_1, theta_2, phi_1, and phi_2 that would define the boundaries of the curved surface [del_theta being theta_2 - theta_1 and so on].

The spherical grid shown at http://www.texample.net/tikz/examples/spherical-and-cartesian-grids/, and copied below, is sort of what I want to create, but that example doesn't use the tikz-3dplot package. I'm not sure how to create this surface. Please help!

I want a shape that looks similar to the red spherical grid
I want a shape that looks similar to the red spherical grid here

My image so far with coordinate system and showing the location of P where I want the spherical shape
My image so far with coordinate system and showing the location of P where I want the spherical shape

\documentclass{article}
\usepackage{verbatim}

\usepackage{tikz}   
\usepackage{tikz-3dplot}
\usepackage[active,tightpage]{preview}  % generates a tightly fitting border around the work
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}

\begin{document}

%Angle Definitions
%-----------------

%set the plot display orientation
%syntax: \tdplotsetdisplay{\theta_d}{\phi_d}
\tdplotsetmaincoords{60}{25}

%define polar coordinates for some vector
%TODO: look into using 3d spherical coordinate system
\pgfmathsetmacro{\rvec}{1}
\pgfmathsetmacro{\thetavec}{50}
\pgfmathsetmacro{\phivec}{15}

%start tikz picture, and use the tdplot_main_coords style to implement the display 
\begin{tikzpicture}[scale=5,tdplot_main_coords]

%set up some coordinates 
%-----------------------
\coordinate (O) at (0,0,0);
\fill (O) circle[radius=0.4pt] node[below left] {O};

%determine a coordinate (P) using (r,\theta,\phi) coordinates.
%syntax: \tdplotsetcoord{Coordinate name without parentheses}{r}{\theta}{\phi}
\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}
\fill (P) circle[radius=0.4pt] node[anchor=south west] {P};

%draw figure contents
%--------------------

%draw the main coordinate system axes
\draw[thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from origin to point (P), projection on xy plane, and a connecting line
\draw[-stealth,color=red] (O) -- (P);
\draw[dashed, color=red] (O) -- (Pxy);
\draw[dashed, color=red] (P) -- (Pxy);

%draw the angle \phi, and label it
%syntax: \tdplotdrawarc[coordinate frame, draw options]{center point}{r}{angle start}{angle end}{label options}{label}
\tdplotdrawarc{(O)}{0.2}{0}{\phivec}{anchor=north}{$\phi$}

%set the rotated coord system so the x'y' plane coincides with the"theta plane" of the main coord system
%syntax: \tdplotsetthetaplanecoords{\phi}
\tdplotsetthetaplanecoords{\phivec}

%draw theta arc and label, using rotated coordinate system
\tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{0.2}{0}{\thetavec}{anchor=south west}{$\theta$}

%draw some dashed arcs, demonstrating direct arc drawing
\draw[dashed,tdplot_rotated_coords] (\rvec,0,0) arc (0:90:\rvec);
\draw[dashed] (\rvec,0,0) arc (0:90:\rvec);

\end{tikzpicture}
\end{document}

Best Answer

So all you really need is a couple of \foreach loops. Note that lines of latitude do not match rotated arcs through the origin (great circle routes).

\documentclass{article}
\usepackage{verbatim}

\usepackage{tikz}   
\usepackage{tikz-3dplot}
\usepackage[active,tightpage]{preview}  % generates a tightly fitting border around the work
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}

\begin{document}

%Angle Definitions
%-----------------

%set the plot display orientation
%syntax: \tdplotsetdisplay{\theta_d}{\phi_d}
\tdplotsetmaincoords{60}{25}

%define polar coordinates for some vector
%TODO: look into using 3d spherical coordinate system
\pgfmathsetmacro{\rvec}{1}
\pgfmathsetmacro{\thetavec}{50}
\pgfmathsetmacro{\phivec}{15}

%start tikz picture, and use the tdplot_main_coords style to implement the display 
\begin{tikzpicture}[scale=5,tdplot_main_coords]

%set up some coordinates 
%-----------------------
\coordinate (O) at (0,0,0);
\fill (O) circle[radius=0.4pt] node[below left] {O};

%determine a coordinate (P) using (r,\theta,\phi) coordinates.
%syntax: \tdplotsetcoord{Coordinate name without parentheses}{r}{\theta}{\phi}
\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}
\fill (P) circle[radius=0.4pt] node[anchor=south west] {P};

%draw figure contents
%--------------------

%draw the main coordinate system axes
\draw[thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from origin to point (P), projection on xy plane, and a connecting line
\draw[-stealth,color=red] (O) -- (P);
\draw[dashed, color=red] (O) -- (Pxy);
\draw[dashed, color=red] (P) -- (Pxy);

%draw the angle \phi, and label it
%syntax: \tdplotdrawarc[coordinate frame, draw options]{center point}{r}{angle start}{angle end}{label options}{label}
\tdplotdrawarc{(O)}{0.2}{0}{\phivec}{anchor=north}{$\phi$}

%set the rotated coord system so the x'y' plane coincides with the"theta plane" of the main coord system
%syntax: \tdplotsetthetaplanecoords{\phi}
\tdplotsetthetaplanecoords{\phivec}

%draw theta arc and label, using rotated coordinate system
\tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{0.2}{0}{\thetavec}{anchor=south west}{$\theta$}

%draw some dashed arcs, demonstrating direct arc drawing
\draw[dashed,tdplot_rotated_coords] (\rvec,0,0) arc (0:90:\rvec);
\draw[dashed] (\rvec,0,0) arc (0:90:\rvec);

\foreach \theta in {80,75,...,\thetavec} {%
  \pgfmathsetmacro{\z}{\rvec*cos(\theta)}
  \pgfmathsetmacro{\r}{\rvec*sin(\theta)}
  \tdplotdrawarc[blue]{(0,0,{\z})}{\r}{15}{30}{}{}
}

\foreach \phi in {15,20,25,30} {\tdplotsetthetaplanecoords{\phi}%
  \tdplotdrawarc[tdplot_rotated_coords,red]{(0,0,0)}{\rvec}{\thetavec}{80}{}{}
}

\end{tikzpicture}
\end{document}

screenshot of demo


To fill the area, I stored points along the border. I also used the plot[smooth cycle] option to make the joins less noticeable. Note that the corner points are entered twice; e.g. (A50) and (B15) are at the same location.

\documentclass{article}
\usepackage{verbatim}

\usepackage{tikz}   
\usepackage{tikz-3dplot}
\usepackage[active,tightpage]{preview}  % generates a tightly fitting border around the work
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}

\begin{document}

%Angle Definitions
%-----------------

%set the plot display orientation
%syntax: \tdplotsetdisplay{\theta_d}{\phi_d}
\tdplotsetmaincoords{60}{25}

%define polar coordinates for some vector
%TODO: look into using 3d spherical coordinate system
\pgfmathsetmacro{\rvec}{1}
\pgfmathsetmacro{\thetavec}{50}
\pgfmathsetmacro{\phivec}{15}

%start tikz picture, and use the tdplot_main_coords style to implement the display 
\begin{tikzpicture}[scale=5,tdplot_main_coords]

%set up some coordinates 
%-----------------------
\coordinate (O) at (0,0,0);
\fill (O) circle[radius=0.4pt] node[below left] {O};

%determine a coordinate (P) using (r,\theta,\phi) coordinates.
%syntax: \tdplotsetcoord{Coordinate name without parentheses}{r}{\theta}{\phi}
\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}
\fill (P) circle[radius=0.4pt] node[anchor=south west] {P};

%draw figure contents
%--------------------

%draw the main coordinate system axes
\draw[thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from origin to point (P), projection on xy plane, and a connecting line
\draw[-stealth,color=red] (O) -- (P);
\draw[dashed, color=red] (O) -- (Pxy);
\draw[dashed, color=red] (P) -- (Pxy);

%draw the angle \phi, and label it
%syntax: \tdplotdrawarc[coordinate frame, draw options]{center point}{r}{angle start}{angle end}{label options}{label}
\tdplotdrawarc{(O)}{0.2}{0}{\phivec}{anchor=north}{$\phi$}

%set the rotated coord system so the x'y' plane coincides with the"theta plane" of the main coord system
%syntax: \tdplotsetthetaplanecoords{\phi}
\tdplotsetthetaplanecoords{\phivec}

%draw theta arc and label, using rotated coordinate system
\tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{0.2}{0}{\thetavec}{anchor=south west}{$\theta$}

%draw some dashed arcs, demonstrating direct arc drawing
\draw[dashed,tdplot_rotated_coords] (\rvec,0,0) arc (0:90:\rvec);
\draw[dashed] (\rvec,0,0) arc (0:90:\rvec);

\foreach \theta in {80,75,70,65,60,55,50} {% left and right borders
  \pgfmathsetmacro{\z}{\rvec*cos(\theta)}
  \pgfmathsetmacro{\r}{\rvec*sin(\theta)}
  \tdplotsetcoord{temp}{\r}{90}{\phivec}
  \path (0,0,{\z}) ++(\phivec:{\r}) coordinate(A\theta);
  \path (0,0,{\z}) ++(30:{\r}) coordinate(C\theta);
}

\foreach \phi in {15,18,21,24,27,30} {% top and bottom borders
  \tdplotsetcoord{B\phi}{\rvec}{\thetavec}{\phi}%
  \tdplotsetcoord{D\phi}{\rvec}{80}{\phi}%
}

\fill[green,opacity=.5] plot[smooth cycle] coordinates{(A80) (A75) (A70) (A65) (A60) (A55) (A50)  
  (B15) (B18) (B21) (B24) (B27) (B30)
  (C50) (C55) (C60) (C65) (C70) (C75) (C80) 
  (D30) (D27) (D24) (D21) (D18) (D15)};

\end{tikzpicture}
\end{document}

second demo