根据视图位置在 TikZ-3dplot 中绘制背景线虚线

根据视图位置在 TikZ-3dplot 中绘制背景线虚线

图中红线部分覆盖了 3d 对象(在背景中)。是否可以绘制虚线的背景部分(或计算“交叉”点)?

之后,我喜欢用透明色填充表面。我担心我必须以其他方式声明表面(如矩形)。

\documentclass[border=5pt]{standalone}   % for LuaLaTeX
\usepackage{pgfplots}                   
\usepackage{tikz, tikz-3dplot}
%\usetikzlibrary{shadows, shadows.blur, shapes, arrows, positioning, circuits.ee.IEC, decorations, matrix, intersections, patterns, 3d}   
%\usepackage[european, cuteinductors]{circuitikz}
\pgfplotsset{compat=newest}
\begin{document}

\tdplotsetmaincoords{70}{10}    % set position of 3d-view
\begin{tikzpicture}[tdplot_main_coords]

% Set parameters
%\newcommand\hexX{1}        -> defined in loop
\newcommand\hexY{0}
%\newcommand\hexZ{0}        -> defined in loop
%\newcommand\hexAng{7.5}    -> defined in loop
\newcommand\lowerinnerrad{6}
\newcommand\lowerouterrad{6.5}
\newcommand\hexlowerHeigth{0}
\newcommand\hexupperHeigth{1}

% Calculate 4*6 Corners and draw connections
\foreach \NAME/\hexX/\hexZ in {li/\lowerinnerrad/\hexlowerHeigth, lo/\lowerouterrad/\hexlowerHeigth, ui/\lowerinnerrad/\hexupperHeigth, uo/\lowerouterrad/\hexupperHeigth}{
    \foreach \i/\n/\Ang in {1/2/0, 3/4/120, 5/6/240}{
        \coordinate (LP_\NAME\i) at ({cos(\Ang+7.5)  *\hexX-sin(\Ang+7.5)  *\hexY}, {sin(\Ang+7.5)  *\hexX+cos(\Ang+7.5)  *\hexY}, \hexZ);          
        \coordinate (LP_\NAME\n) at ({cos(\Ang+112.5)*\hexX-sin(\Ang+112.5)*\hexY}, {sin(\Ang+112.5)*\hexX+cos(\Ang+112.5)*\hexY}, \hexZ);  
    }
    \draw[-] (LP_\NAME1)--(LP_\NAME2)--(LP_\NAME3)--(LP_\NAME4)--(LP_\NAME5)--(LP_\NAME6)--cycle;
}

% Draw connections of each corner and numbering
\foreach \Number in {1,2,...,6}{
        \draw[-] (LP_li\Number)--(LP_lo\Number)--(LP_uo\Number)--(LP_ui\Number)--cycle;
        \node[label=above:{\Number}] (a\Number) at (LP_uo\Number) {};       % indicating corners only
}

\draw[-, red] (LP_li3)--(LP_li4); % example: partly covered line

\end{tikzpicture}
\end{document}

3D 对象

编辑1

我稍微扩展了这个例子(只需在上面插入代码\end{tikzpicture})。添加了一个圆圈来显示如何计算每个六边形的 6 个点。另外,顶部的平面被填充了。但线条的问题仍然存在……

\foreach \Ang in {0,1,2,...,360}{                                                          %Calculate circle points
    \coordinate (LP_circ\Ang) at ({cos(\Ang)  *\lowerouterrad-sin(\Ang)  *\hexY}, {sin(\Ang)  *\lowerouterrad+cos(\Ang)  *\hexY}, \hexupperHeigth);             
}

\foreach \Ang in {0,1,2,...,359}{
    \pgfmathtruncatemacro{\Numberplusone}{\Ang+1}                                       % Calculate following point number
    \draw[-, blue!80!black](LP_circ\Ang)--(LP_circ\Numberplusone);
}

\fill[green, opacity=0.3] (LP_lo4) -- (LP_uo4) -- (LP_uo5) -- (LP_lo5) -- cycle;        % Example for one segment, like loop below

\foreach \Number in {1,2,...,6}{                                                        % Fill top segments
    \pgfmathtruncatemacro{\Numberplusone}{\Number+1}                                    % Calculate following point number
    \ifthenelse{\equal{\Numberplusone}{7}}{\pgfmathtruncatemacro{\Numberplusone}{1}}{}  % Point 7 does not exist, will be changed to 1

    \fill[blue, opacity=0.3] (LP_uo\Number) -- (LP_ui\Number) -- (LP_ui\Numberplusone) -- (LP_uo\Numberplusone) -- cycle;
}

版本2

编辑2

现在我对其进行了进一步的编辑,现在看起来好多了,但虚线还是不错的,还有如何在“中间”画线或箭头的解决方案(见图)。还有一个问题是,如果你改变视角,你就必须画一幅几乎全新的图片。希望任何人都有遇到过这样的(3d)问题的经验。

\documentclass[border=5pt]{standalone}   % for LuaLaTeX
\usepackage{pgfplots}                   
\usepackage{tikz, tikz-3dplot}
\usetikzlibrary{patterns}  
\pgfplotsset{compat=newest}
\begin{document}

\tdplotsetmaincoords{70}{10}    % set position of 3d-view
\begin{tikzpicture}[tdplot_main_coords]

% Set parameters------------------------------------------------------------------------------------------------------------
% First Platform LP
%\newcommand\HexX{1}        -> defined in loop
\newcommand\HexY{0}
%\newcommand\HexZ{0}        -> defined in loop
%\newcommand\hexAng{7.5}    -> defined in loop
\newcommand\LowerInnerRad{6}
\newcommand\LowerMiddleRad{6.4}
\newcommand\LowerOuterRad{6.8}
\newcommand\HexLowerHeigthLP{0}
\newcommand\HexUpperHeigthLP{1}



% Coordinate System------------------------------------------------------------------------------------------------------------
\foreach \Ang in {0,1,2,...,360}{                                                      %Calculate circle points
        \coordinate (LP_circ\Ang) at ({cos(\Ang)  *\LowerOuterRad-sin(\Ang)  *\HexY}, {sin(\Ang)  *\LowerOuterRad+cos(\Ang)  *\HexY}, 0);               
}


\draw[-, dotted] \foreach \x [remember=\x as \lastx (initially 0)] in {1,...,360}{(LP_circ\lastx) -- (LP_circ\x)} -- (LP_circ0);
%\draw[-, fill=green] \foreach \Ang in {1,...,359}{\pgfmathtruncatemacro{\NumberPlusOne}{\Ang+1}
%(LP_circ\Ang) -- (LP_circ\NumberPlusOne)}-- cycle;
\fill[pattern color = gray, pattern = north east lines] (LP_circ0)--(LP_circ10)--(LP_circ20)--(LP_circ30)--(LP_circ40)--(LP_circ50)--(LP_circ60)--(LP_circ70)--(LP_circ80)--(LP_circ90)--(LP_circ100)--(LP_circ110)--(LP_circ120)--(LP_circ130)--(LP_circ140)--(LP_circ150)--(LP_circ160)--(LP_circ170)--(LP_circ180)--(LP_circ190)--(LP_circ200)--(LP_circ210)--(LP_circ220)--(LP_circ230)--(LP_circ240)--(LP_circ250)--(LP_circ260)--(LP_circ270)--(LP_circ280)--(LP_circ290)--(LP_circ300)--(LP_circ310)--(LP_circ320)--(LP_circ330)--(LP_circ340)--(LP_circ350)--cycle; % in loop it didn't work...


\draw[-, dashed, thick] (0,0,0)--({cos(120)  *\LowerOuterRad-sin(120)  *\HexY}, {sin(120)  *\LowerOuterRad+cos(120)  *\HexY}, 0);
\draw[-, dashed, thick] (0,0,0)--({cos(240)  *\LowerOuterRad-sin(240)  *\HexY}, {sin(240)  *\LowerOuterRad+cos(240)  *\HexY}, 0);


\draw[-stealth, thick] (0,0,0)--(8,0, 0) node[anchor=west]{$x$};
\draw[-stealth, thick] (0,0,0)--(0,-8, 0) node[anchor=north]{$y$};
\draw[-stealth, thick] (0,0,0)--(0,0,-2) node[anchor=north]{$z$};
    \draw[-stealth, thick, red] (0,0,-1.8)--(0,0, 4) node[anchor=south]{arrow should be ``surrounded" by hexagon};


% Platform------------------------------------------------------------------------------------------------------------
% Calculate 4*6 Corners and draw connections
\foreach \NAME/\HexX/\HexZ in {li/\LowerInnerRad/\HexLowerHeigthLP, lo/\LowerOuterRad/\HexLowerHeigthLP, ui/\LowerInnerRad/\HexUpperHeigthLP, uo/\LowerOuterRad/\HexUpperHeigthLP}{
    \foreach \i/\n/\Ang in {1/2/0, 3/4/120, 5/6/240}{
        \coordinate (LP_\NAME\i) at ({cos(\Ang+7.5)  *\HexX-sin(\Ang+7.5)  *\HexY}, {sin(\Ang+7.5)  *\HexX+cos(\Ang+7.5)  *\HexY}, \HexZ);          
        \coordinate (LP_\NAME\n) at ({cos(\Ang+112.5)*\HexX-sin(\Ang+112.5)*\HexY}, {sin(\Ang+112.5)*\HexX+cos(\Ang+112.5)*\HexY}, \HexZ);  
    }
    %\draw[-] (LP_\NAME1)--(LP_\NAME2)--(LP_\NAME3)--(LP_\NAME4)--(LP_\NAME5)--(LP_\NAME6)--cycle;
}

\foreach \NAMEa/\NAMEb/\op in {uo/ui/0.2, lo/li/0.8, ui/li/0.2, uo/lo/0.2}{                                     % Fill 4 outside walls  
    \foreach \Number in {1,2,...,6}{                                                        % Fill 6 segments (of the top plateau, for example)
        \pgfmathtruncatemacro{\NumberPlusOne}{\Number+1}                                    % Calculate following point number
        \ifthenelse{\equal{\NumberPlusOne}{7}}{\pgfmathtruncatemacro{\NumberPlusOne}{1}}{}  % Point 7 does not exist, will be changed to 1

        \fill[blue, opacity=\op] (LP_\NAMEa\Number) -- (LP_\NAMEb\Number) -- (LP_\NAMEb\NumberPlusOne) -- (LP_\NAMEa\NumberPlusOne) -- cycle;
    }
}

% Draw connections of each corner and numbering
\foreach \Number in {1,2,...,6}{
        \draw[-] (LP_li\Number)--(LP_lo\Number)--(LP_uo\Number)--(LP_ui\Number)--cycle;
        \node[label=above:{\Number}] (a\Number) at (LP_uo\Number) {};       % indicating corners only
}

\foreach \NAME in {li, lo, ui, uo}{
    \draw[-] (LP_\NAME1)--(LP_\NAME2)--(LP_\NAME3)--(LP_\NAME4)--(LP_\NAME5)--(LP_\NAME6)--cycle;
}

\end{tikzpicture}
\end{document}

版本 3

答案1

TikZ 不理解任何有关 z 顺序的知识(尽管 Christian Feuersanger 花了很大力气实现了 3D 图的有限版本pgfplots)。

然而,更强大(但在其他方面不太用户友好)的工具可以很容易地做到这一点,例如 PSTricks、Asymptote 等。

最好的办法是使用另一个工具绘制图像,然后如果您愿意的话,将其转换为具有固定视角的 TikZ 代码。

相关内容