2

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}
  • The are many example here for drawing spheres. See tex.stackexchange.com/questions/196902/… for example. – John Kormylo May 20 '17 at 19:12
  • @JohnKormylo I know how to draw arcs using 3d polar coordinates (I've done that for labeling theta for example). I just don't know how I would draw 4 arcs and then colour the curved shape so it looks 3 dimensional. I would also have to define rotations before drawing each of the four arcs. 'Scope' is new to me, I will check it out. – bluprince13 May 21 '17 at 17:06
  • You can jump out of the tikz-3dplot coordinate system temporarily to draw the sphere. Wouldn't that do what you want? Have you considered using software which knows about 3D? – cfr May 21 '17 at 20:25
  • @cfr well, I don't want to draw a sphere, only a slice of it. What software are you referring to? CAD? – bluprince13 May 21 '17 at 21:58
  • 1
    Things like asymptote know about 3D, whereas TikZ/PGF, as we know, does not. – cfr May 21 '17 at 22:44
5

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

  • Thanks John, that helps! But, what I was after is a filled in shape than a grid. Is it perhaps a minor tweak to achieve that? – bluprince13 May 22 '17 at 14:56
  • I suggested an edit with a nicer screenshot. Why does yours have such ungly alising? Just curious, I see these around here sometimes. – lblb May 22 '17 at 16:26
  • @lblb - It's not a screen shot. I used Acrobat to save the PDF as PNG. I suppose I could get a higher resolution, but I do this a lot and this is faster. – John Kormylo May 22 '17 at 22:21
  • @bluprince13 - Tricky! To fill things you need to set up a path around the perimeter, but in this case the perimeter involves changing the coordinates system several times. – John Kormylo May 22 '17 at 22:30
  • Ah! I was afraid of that. Thanks though. I guess I should I use asymptote rather than TikZ for this. – bluprince13 May 22 '17 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.