我可以用这段代码来绘制正多面体吗?

我可以用这段代码来绘制正多面体吗?

在这段代码中,我画了一个正多面体,其底面是一个等边三角形。

 \documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot} 
\begin{document}
    \tdplotsetmaincoords{70}{100}
    \begin{tikzpicture}[tdplot_main_coords,line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},
        declare function={r=3;r1=2;h=3;}]
        \path
        ({r*cos(0)},{r*sin(0)},0) coordinate (A)
        ({r*cos(120)},{r*sin(120)},0) coordinate (B)
        ({r*cos(240)},{r*sin(240)},0) coordinate (C)
        ({r1*cos(0)},{r*sin(0)},h) coordinate (D)
    ({r1*cos(120)},{r1*sin(120)},h) coordinate (E)
    ({r1*cos(240)},{r1*sin(240)},h) coordinate (F);
\draw  (C)-- (A)--(B) 
(D)--(E)--(F)--cycle
(A) -- (D) (B) --(E) (C) -- (F)
; 
\draw [dashed] (C) -- (B);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

我怎样才能概括这个具有两个基础的代码n - regular polygon

答案1

有一种简单的方法可以用循环放置所有坐标\foreach。图形也可以用这种方式绘制,但会失去可见性。

例如:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d} % for 'canvas is...' options

\begin{document}
\tdplotsetmaincoords{70}{100}
\pgfmathsetmacro\n{7} % number of sides
\begin{tikzpicture}[tdplot_main_coords,line cap=butt,line join=round,%c/.style={circle,fill,inner sep=1pt},
                    declare function={r=3;r1=2;h=3;}]
\foreach\i in {1,...,\n}
{
  \coordinate (A\i) at (360*\i/\n:r);
  \coordinate[canvas is xy plane at z=h] (B\i) at (360*\i/\n:r1);
  \draw (A\i) -- (B\i);
}
\foreach\i in {A,B}
  \draw (\i1) foreach\j in {2,...,\n} {-- (\i\j)} -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

更新 1(对可见性有一点帮助):旋转图形,使顶点 A1 成为右侧第一个不可见顶点。然后设置\vl为左侧第一个可见顶点的编号。其余为“自动”。

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d} % for 'canvas is...' options

\begin{document}
\tdplotsetmaincoords{70}{10} % <--- rotation
\pgfmathsetmacro\n{9}        % number of sides
\pgfmathsetmacro\vl{5}       % visibility limit
\pgfmathtruncatemacro\vv{\vl+1}
\begin{tikzpicture}[tdplot_main_coords,line cap=butt,line join=round,
                    declare function={r=3;r1=2;h=3;}]
\foreach\i in {1,...,\n}
{
  \coordinate (A\i) at (360*\i/\n:r);
  \coordinate[canvas is xy plane at z=h] (B\i) at (360*\i/\n:r1);
  \pgfmathtruncatemacro\ii{\i+1}
  \draw[\ifnum\i<\vl dashed\fi] (A\i) -- (B\i);
}
\draw (B1) foreach\j in {2,...,\n} {-- (B\j)} -- cycle;
\foreach\j in {1,...,\n}
  \pgfmathtruncatemacro\i{mod(\j,\n)+1}
  \draw[\ifnum\i<\vv dashed\fi] (A\i) -- (A\j);
% only for help:
\foreach\i in {1,...,\n}
  \node[red,below] at (A\i) {$\i$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

更新 2:只是改变

\pgfmathsetmacro\n{3}  % number of sides
\pgfmathsetmacro\vl{2} % visibility limit

输出将是 在此处输入图片描述

答案2

https://texample.net/media/tikz/examples/TEX/cone.tex修改。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt} 
\usepackage{tikz-3dplot}
\begin{document}

\tdplotsetmaincoords{70}{0}
\begin{tikzpicture}[tdplot_main_coords]
\def\RI{2}
\def\RII{1.25}

\draw  (\RI,0)
  \foreach \x in {0,300,240,180} { --  (\x:\RI) node at (\x:\RI) (R1-\x) {} };
\draw  (R1-0.center)
  \foreach \x in {60,120,180} { --  (\x:\RI) node at (\x:\RI) (R1-\x) {} };
\path  (\RI,0)
  \foreach \x in {0,60,120,180,240,300} { --  (\x:\RI)};
\begin{scope}[yshift=2cm]
\draw  (\RII,0)
  \foreach \x in {0,60,120,180,240,300,360} { --  (\x:\RII) node at (\x:\RII) (R2-\x) {}};
\end{scope}

\foreach \x in {0,180,240,300} { \draw (R1-\x.center)--(R2-\x.center); };
\foreach \x in {60,120} { \draw  (R1-\x.center)--(R2-\x.center); };
\end{tikzpicture}

\end{document}

答案3

您可以尝试此代码并使用 \pgfmathtruncatemacro{\Ncorners}{3}。您可以3用其他数字更改该数字。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}%
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
    \begin{scope}[3d/install view={phi=110,theta=70},
            declare function={R=3.5;R1=2;               H=3;
            },
            dot/.style={circle,fill,inner sep=1.2pt,label=above:{#1}}]
            \pgfmathtruncatemacro{\Ncorners}{3} 
            \path
            (0,0,0) coordinate (O)  
            (0,0,H) coordinate (S)
            (0,0,H/2) coordinate (C)        
            foreach \X in {1,...,\Ncorners}
            {({R*cos(\X*360/\Ncorners)},{R*sin(\X*360/\Ncorners)},0)
                coordinate (A\X)
            }
            foreach \X in {1,...,\Ncorners}
            {({R1*cos(\X*360/\Ncorners)},{R1*sin(\X*360/\Ncorners)},H) coordinate (A'\X)
            };
            \makeatletter
            \edef\myLetter{\@Alph{\Ncorners}}
            \makeatother    
            
            \edef\mycorners{{(A'1)}}        
            \tikzset{add corner/.code={\edef\mycorners{\mycorners,{(A'#1)}}},
                add corner/.list={2,...,\Ncorners}}     
            \begin{scope}[3d/polyhedron/.cd,O={(C)},fore/.append style={fill=none},
                back/.append style={3d/hidden},
                fore/.append style={3d/visible,fill=none},
                edges have complete dashes]
                \foreach \X [remember=\X as \LastX (initially \Ncorners)] in {1,...,\Ncorners}
                {\tikzset{3d/polyhedron/draw face with corners/.expanded={{(A\LastX)},{(A'\LastX)},{(A'\X)},(A\X)}}}
                \tikzset{3d/polyhedron/draw face with corners/.expanded={\mycorners}}
            \end{scope}         
        \end{scope}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

在此处输入图片描述

相关内容