在凸面上绘制二维方格(弹簧+球)

在凸面上绘制二维方格(弹簧+球)

我需要帮助在凸面(例如球面)上绘制由弹簧和球组成的 2D 方形格子(见图)。您能否告诉我是否可以在 PSTricks 或 TikZ 中制作这种格子?非常感谢。

我在 PDF 中链接了 [1] 一个相关示例,其中有一个球面上的网格。

格子弹簧

[1]http://www.texample.net/media/tikz/examples/PDF/spherical-and-cartesian-grids.pdf

答案1

欢迎来到 TeX.SE!这里有一个提议。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\clip (-1,-1) rectangle (11,11);
\foreach \X in {-2,0,...,10}
{\foreach \Y in {-2,0,...,10}
 {\draw[decorate,decoration={coil,aspect=0.5,amplitude=1.5mm, segment
length=1.5mm}] (\X,\Y) -- ++(0,2) -- ++(2,0);
\node[circle,text=white,font=\sffamily\bfseries\large,inner
color=blue,outer color=black] at (\X,\Y) {+};}}
\end{tikzpicture}
\end{document}

在此处输入图片描述

将其嵌入到球面坐标系中很简单。(非常感谢@caverac 指出问题的真正含义。)

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{decorations.pathmorphing,calc}
\makeatletter
%from https://tex.stackexchange.com/a/375604/121799
%along x axis
\define@key{x sphericalkeys}{radius}{\def\myradius{#1}}
\define@key{x sphericalkeys}{theta}{\def\mytheta{#1}}
\define@key{x sphericalkeys}{phi}{\def\myphi{#1}}
\tikzdeclarecoordinatesystem{x spherical}{% %%%rotation around x
    \setkeys{x sphericalkeys}{#1}%
    \pgfpointxyz{\myradius*cos(\mytheta)}{\myradius*sin(\mytheta)*cos(\myphi)}{\myradius*sin(\mytheta)*sin(\myphi)}}

%along y axis
\define@key{y sphericalkeys}{radius}{\def\myradius{#1}}
\define@key{y sphericalkeys}{theta}{\def\mytheta{#1}}
\define@key{y sphericalkeys}{phi}{\def\myphi{#1}}
\tikzdeclarecoordinatesystem{y spherical}{% %%%rotation around x
    \setkeys{y sphericalkeys}{#1}%
    \pgfpointxyz{\myradius*sin(\mytheta)*cos(\myphi)}{\myradius*cos(\mytheta)}{\myradius*sin(\mytheta)*sin(\myphi)}}

%along z axis
\define@key{z sphericalkeys}{radius}{\def\myradius{#1}}
\define@key{z sphericalkeys}{theta}{\def\mytheta{#1}}
\define@key{z sphericalkeys}{phi}{\def\myphi{#1}}
\tikzdeclarecoordinatesystem{z spherical}{% %%%rotation around x
    \setkeys{z sphericalkeys}{#1}%
    \pgfpointxyz{\myradius*sin(\mytheta)*cos(\myphi)}{\myradius*sin(\mytheta)*sin(\myphi)}{\myradius*cos(\mytheta)}}


\makeatother % https://tex.stackexchange.com/a/438695/121799

\begin{document}
\tdplotsetmaincoords{70}{130}
\begin{tikzpicture}[tdplot_main_coords]
\pgfmathsetmacro{\R}{16}
    \path[clip]
    plot[variable=\x,domain=2.5:52.5]
     (z spherical cs:radius=\R,theta=80-2.5,phi=\x)
     -- plot[variable=\x,domain=2.5:52.5]
     (z spherical cs:radius=\R,theta=80-\x,phi=52.5)
     -- plot[variable=\x,domain=52.5:2.5]
     (z spherical cs:radius=\R,theta=80-52.5,phi=\x)
     -- plot[variable=\x,domain=52.5:2.5]
     (z spherical cs:radius=\R,theta=80-\x,phi=2.5);
    \foreach \X in {0,5,...,55}
     {\draw[decorate,decoration={coil,aspect=0.5,amplitude=1.5mm, segment
length=1.5mm}] plot[variable=\x,domain=0:55]
     (z spherical cs:radius=\R,theta=80-\X,phi=\x);
     \draw[decorate,decoration={coil,aspect=0.5,amplitude=1.5mm, segment
length=1.5mm}] plot[variable=\x,domain=0:55]
     (z spherical cs:radius=\R,theta=80-\x,phi=\X);
     }
    \foreach \X in {5,10,...,50}
    {\foreach \Y in {5,10,...,50}
     {\path let \p1=($(z spherical cs:radius=\R,theta=80-\X+1,phi=\Y)-(z
     spherical cs:radius=\R,theta=80-\X,phi=\Y)$),
     \p2=($(z spherical cs:radius=\R,theta=80-\X,phi=\Y+1)-(z
     spherical cs:radius=\R,theta=80-\X,phi=\Y)$),
     \n1={veclen(\x1,\y1)/12},\n2={veclen(\x2,\y2)/12},\n3={sqrt(\n1*\n2)}
     in %\pgfextra{\typeout{\X,\Y:\n1,\n2}}
     node[scale=\n3,transform shape,circle,text=white,font=\sffamily\bfseries\large,inner
color=blue,outer color=black] at (z spherical cs:radius=\R,theta=80-\X,phi=\Y) {+};}}
\end{tikzpicture}
\end{document}

在此处输入图片描述

至于你的额外要求:是的,这是可能的。但是,事情取决于你最终真正想要实现的目标。这里我给出了一些例子,这些例子很大程度上依赖于Fritz 的精彩回答。然而,当我尝试通过在不同层上绘制东西来完善它时,我遇到了意想不到的问题。无论如何,我不知道你的真正目的是什么。(参见例如这篇很棒的文章以获得更多可能性。)我恳请您以新问题的形式提出更多请求,其中列出了所有要求。毕竟,提问是免费的。我留给你的是

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{pgfplots}
\usepackage{xxcolor}
\pgfplotsset{compat=1.16}
\usetikzlibrary{decorations.pathmorphing,decorations.markings}
% Declare nice sphere shading: http://tex.stackexchange.com/a/54239/12440
\pgfdeclareradialshading[tikz@ball]{ball}{\pgfqpoint{0bp}{0bp}}{%
 color(0bp)=(tikz@ball!0!white);
 color(7bp)=(tikz@ball!0!white);
 color(15bp)=(tikz@ball!70!black);
 color(20bp)=(black!70);
 color(30bp)=(black!70)}
\makeatother

% Style to set TikZ camera angle, like PGFPlots `view`
\tikzset{viewport/.style 2 args={
    x={({cos(-#1)*1cm},{sin(-#1)*sin(#2)*1cm})},
    y={({-sin(-#1)*1cm},{cos(-#1)*sin(#2)*1cm})},
    z={(0,{cos(#2)*1cm})}
}}

% Styles to plot only points that are before or behind the sphere.
\pgfplotsset{only foreground/.style={
    restrict expr to domain={rawx*\CameraX + rawy*\CameraY + rawz*\CameraZ}{-0.05:100},
}}
\pgfplotsset{only background/.style={
    restrict expr to domain={rawx*\CameraX + rawy*\CameraY + rawz*\CameraZ}{-100:0.05}
}}

% Automatically plot transparent lines in background and solid lines in foreground
\def\addFGBGplot[#1]#2;{
    \addplot3[#1,only background, opacity=0.25] #2;
    \addplot3[#1,only foreground] #2;
}

% attempt to do similar things for discrete plots
\def\addFGBGSampleplot[#1]#2;{
    %\addplot3[#1,only background,gray!50] #2;
    \addplot3[#1,only foreground] #2;
}


\newcommand{\ViewAzimuth}{-30}
\newcommand{\ViewElevation}{30}

\tikzset{spring/.style={decorate,decoration={coil,aspect=0.5,amplitude=1.5mm, segment
length=1.5mm}}}
% pgfmanual p. 1087
\pgfdeclareradialshading{ballshading}{\pgfpoint{-10bp}{10bp}}
 {color(0bp)=(cyan!15!white); color(9bp)=(cyan!75!white);
 color(18bp)=(cyan!70!black); color(25bp)=(cyan!50!black); color(50bp)=(black)}

\pgfdeclareplotmark{crystal ball}{\pgfpathcircle{\pgfpoint{0ex}{0ex}}{2ex}
  \pgfshadepath{ballshading}{0}
  \pgfusepath{}}
\begin{document}
\begin{tikzpicture}
    % Compute camera unit vector for calculating depth
    \pgfmathsetmacro{\CameraX}{sin(\ViewAzimuth)*cos(\ViewElevation)}
    \pgfmathsetmacro{\CameraY}{-cos(\ViewAzimuth)*cos(\ViewElevation)}
    \pgfmathsetmacro{\CameraZ}{sin(\ViewElevation)}
    \pgfmathsetmacro{\R}{12}
    \path[use as bounding box] (-\R,-\R) rectangle (\R,\R); % Avoid jittering animation
    % Draw a nice looking sphere
    \begin{scope}
        \clip (0,0) circle (\R);
        \begin{scope}[transform canvas={rotate=-20}]
            \shade [ball color=white] (0,0.5) ellipse (\R*1.8 and \R*1.5);
        \end{scope}
    \end{scope}
    \begin{axis}[
        hide axis,
        view={\ViewAzimuth}{\ViewElevation},     % Set view angle
        every axis plot/.style={very thin},
        disabledatascaling,                      % Align PGFPlots coordinates with TikZ
        anchor=origin,                           % Align PGFPlots coordinates with TikZ
        viewport={\ViewAzimuth}{\ViewElevation}, % Align PGFPlots coordinates with TikZ
    ]
        % plot latitude circles
        \pgfplotsinvokeforeach{-75,-45,...,75}
        {\addFGBGplot[spring,domain=0:2*pi, samples=51, samples y=1]
        ({\R*cos(#1)*cos(deg(x))}, {\R*cos(#1)*sin(deg(x))}, {\R*sin(#1)});}
        % plot longitude circles
        \pgfplotsinvokeforeach{0,30,...,150}
        {\addFGBGplot[spring,domain=0:2*pi, samples=51, samples y=1]
        ({\R*cos(#1)*cos(deg(x))}, {\R*sin(#1)*cos(deg(x))}, {\R*sin(deg(x))});     
        }
        % plot longitude circles
        \pgfplotsinvokeforeach{0,30,...,330}
        {\addFGBGSampleplot[only marks,mark=crystal ball,samples at={-75,-45,...,75}]
        ({\R*cos(#1)*cos(x)}, {\R*sin(#1)*cos(x)}, {\R*sin(x)});        
        }
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容