Wednesday , July 24 2019
Home / Uncategorized / tikz pgf – How to make a 2D sphere with decreasing radial gradient – TeX

tikz pgf – How to make a 2D sphere with decreasing radial gradient – TeX



If you want only a radial fading, then the example just before section 23.4.2 may be the way to go.

documentclass[a4paper]{article}
usepackage{tikz} 
usepackage{tikz-3dplot} 
usetikzlibrary{fadings}
tikzfading[name=fade out,
inner color=transparent!0,
outer color=transparent!100]

begin{document}
  tdplotsetmaincoords{45}{135}
  pgfmathsetmacro{rvec}{.8}
  pgfmathsetmacro{thetavec}{45}
  pgfmathsetmacro{phivec}{75}

  begin{tikzpicture}[scale=9,tdplot_main_coords]
    fill[tdplot_screen_coords,gray!40!black,%ball color = gray,
    path fading=fade out] (0,0) circle (rvec);
    coordinate (O) at (0,0,0);
    tdplotsetcoord{P}{rvec}{thetavec}{phivec}

    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,-1) -- (0,0,1) node[anchor=south]{$z$};
    draw[-stealth,color=black] (O) -- (P) node[midway,above] {$r$};
    draw[dashed, color=red] (O) -- (Pxy);
    draw[dashed, color=red] (P) -- (Pxy);
    tdplotdrawarc{(O)}{0.2}{0}{phivec}{anchor=north}{$phi$}

    tdplotsetthetaplanecoords{phivec}
    tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{0.5}{thetavec}{90}{anchor=south west}{$theta$}
    tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{rvec}{-180}{180}{anchor=south west}{}
  end{tikzpicture} 
end{document}

enter image description here

Another option is to slightly slightly modify the fuzzy circles from the shadings library. (I merely copied and slightly modified the fuzzy circle fading from the library to give you immediate access to the transparency levels at different distances. I am not claiming that my choice is particularly nice, but in the end you need to decide yourself what suits you best. The number 23 does not really represent a percentage, but if you play basketball you may understand why I picked it. 😉

documentclass[a4paper]{article}
usepackage{tikz} 
usepackage{tikz-3dplot} 
usetikzlibrary{fadings}
makeatletter
pgfdeclareradialshading{tikz@lib@fade@circle@23}{pgfpointorigin}{
  color(0pt)=(pgftransparent!0); color(20bp)=(pgftransparent!50);
  color(25bp)=(pgftransparent!92); color(50bp)=(pgftransparent!100)}
pgfdeclarefading{circle with fuzzy edge 23 percent}{pgfuseshading{tikz@lib@fade@circle@23}}
makeatother
tikzfading[name=fade out,
inner color=transparent!0,
outer color=transparent!100]

begin{document}
  tdplotsetmaincoords{45}{135}
  pgfmathsetmacro{rvec}{.8}
  pgfmathsetmacro{thetavec}{45}
  pgfmathsetmacro{phivec}{75}

  begin{tikzpicture}[scale=9,tdplot_main_coords]
    shade[tdplot_screen_coords,ball color=gray!60,
    path fading={circle with fuzzy edge 23 percent}] (0,0) circle (rvec);
    coordinate (O) at (0,0,0);
    tdplotsetcoord{P}{rvec}{thetavec}{phivec}

    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,-1) -- (0,0,1) node[anchor=south]{$z$};
    draw[-stealth,color=black] (O) -- (P) node[midway,above] {$r$};
    draw[dashed, color=red] (O) -- (Pxy);
    draw[dashed, color=red] (P) -- (Pxy);
    tdplotdrawarc{(O)}{0.2}{0}{phivec}{anchor=north}{$phi$}

    tdplotsetthetaplanecoords{phivec}
    tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{0.5}{thetavec}{90}{anchor=south west}{$theta$}
    tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{rvec}{-180}{180}{anchor=south west}{}
  end{tikzpicture} 
end{document}

enter image description here


Source link

Leave a Reply

Your email address will not be published.