如何用 tikz 绘制半圆柱体

如何用 tikz 绘制半圆柱体

我想画下面的图。我们如何才能自动计算缠结角度。 在此处输入图片描述

%https://tex.stackexchange.com/questions/564162/drawing-half-cylinder-under-a-pyramid-through-tikz

先感谢您!

平均能量损失

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{3d,perspective}
\begin{document}
\begin{tikzpicture}[rotate around y=60]
\def\radius{2}
\def\height{4}
\def\mark{0.2}
\def\ang{60}
\draw[thick] (-\radius,0,\radius) -- (\radius,0,\radius) -- (\radius,0,-\radius)--(-\radius,0,-\radius)--cycle;
\begin{scope}[rotate around y=-90,shift={(\radius,0,\radius)}]
  \draw[thick] (0,0,0) arc(0:-180:\radius);
\end{scope}
\begin{scope}[rotate around y=90,shift={(\radius,0,\radius)}]
  \draw[densely dashed, thick,black!80] (0,0,0) arc(0:-180+\ang:\radius);
\end{scope}
\begin{scope}[rotate around y=-90,shift={(\radius,0,-\radius)}]
  \draw[thick] (0,0,0) arc (0:-\ang:\radius) -- ++(0,0,2*\radius);
\end{scope}
\end{tikzpicture}
\end{document}

我写的另一种方法

\documentclass[border=3pt,tikz]{standalone}
\usetikzlibrary{calc,angles}
\begin{document}
\begin{tikzpicture}[declare function={goc=10;r=2.5;l=4;}]
  \draw ({180-goc}:r)  arc ({180-goc}:{360-goc}:r);
  \draw[shift={(l,0)}] (270:r) arc ({270}:{360-goc}:r)
  ({180-goc}:r) coordinate (A) arc ({180-goc}:{180+0.25*goc}:r);
  \draw[shift={(l,0)},dashed] ({180+0.25*goc}:r) arc ({180+0.25*goc}:{270-goc}:r);
  \draw (270:r)--+(l,0)
  ({360-goc}:r)--({180-goc}:r)--+(l,0)
  ({360-goc}:r)--+(l,0)--(A);
\end{tikzpicture}
\end{document}

答案1

纯粹为了比较,这里有一个简单的元帖子图表的版本。但我赶紧补充说,MP 没有任何内置的 3D 工作支持——我使用路径的几何形状halfcircle来计算此简单绘图中的隐藏线。

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    path a, b; 
    numeric r; r = 13;
    a = halfcircle rotated (180-r) scaled 144 -- cycle;
    b = a shifted 300 right;
   
    numeric s, t; 
    s = 2r / 45;
    t = directiontime right of a;

    draw subpath (0, s) of b withpen pencircle scaled 1/4;
    draw subpath (s, t) of b dashed evenly scaled 1/2 withcolor 3/4;
    draw subpath (t, 5) of b;
    for $ = 0, t, 4:
        draw point $ of a -- point $ of b;
    endfor
    draw a; 
    for $ = a, b:
        draw point -1/2 of $ withpen pencircle scaled 2;
    endfor

    label.llft("$r=" & decimal r & "$", urcorner currentpicture); 
endfig;
\end{mplibcode}
\end{document}

它包含在内,luamplib因此您需要使用 进行编译lualatex。以下是参数的各种值的输出r

半圆柱旋转 13

半圆柱旋转 21

半圆柱旋转 34

答案2

草图,而不是完整的解决方案。您要使用的的是\usetikzlibrary{intersections}

这里有几篇交叉文章,其中这篇可能很有教育意义:有人可以帮我求出两个交点吗?. 另请参阅pgf-manual,,chapter 13.3.2 Intersections of Arbitrary Paths第 144 页及后续页面。

如果您想将解决方案从链接转移到您的代码中,我建议您单独列出并命名两条路径(线)和您想要相交的圆弧。接下来可以使用交点来绘制跨线或虚线,因此我也会重新排列\draw命令的顺序。您的函数可能已经过时了?

绘制解决方案草图,并进行手动编辑:

解决方案草图

% ~~~ analysing existing code ~~~~~~~~
\documentclass[border=3pt]{standalone}

\usepackage{tikz}
%\usetikzlibrary{calc, angles}
\usetikzlibrary{intersections}% <<<

\begin{document}

 \begin{tikzpicture}[declare function={goc=10;r=2.5;l=4;}]
   \draw ({180-goc}:r)  arc ({180-goc}:{360-goc}:r);
   % ~~~ visible arcs in the back ~~~~
   \draw[shift={(l,0)}] (270:r) arc ({270}:{360-goc}:r)
   ({180-goc}:r) coordinate (A) arc ({180-goc}:{180+0.25*goc}:r);
  
   % ~~~ too many lines drawn at ones wrt. intersections ~~~~~~~~~~
   \draw [red] 
   (270:r)--+(l,0) % line at bottom
   ({360-goc}:r)--({180-goc}:r)--+(l,0)% upper right 2 lines
   ({360-goc}:r)--+(l,0)--(A)% upper left 2 lines
   ;
   % ~~~ dashed part in the back ~~~~~
   \draw[shift={(l,0)},dashed] ({180+0.25*goc}:r) arc ({180+0.25*goc}:{270-goc}:r);

 \end{tikzpicture}

\end{document}

答案3

尝试进一步理解海报的最后答案。我引入的更改:

  • 因为article我要绘制多个形状
  • 将 -code移动tikz到名为 的新宏 halfcycl;注意%每行末尾的 - 符号
  • 引入一些\def语句;想法是能够“只在一个地方改变”,并通过使用“常量”来减少一些视觉噪音
  • 添加了一些评论以更好地跟进你的绘画
  • 最后,多次调用halfcycle不同的参数,打印在前面

结果:因此,很多时候它运行良好,并且留下了一些改进的空间。

结果

%\documentclass[border=3pt,tikz]{standalone}
\documentclass[12pt]{article}
\usepackage{tikz}

\newcommand\halfcyl[3]{% goc / r / L
  \begin{tikzpicture}[declare function={goc=#1;r=#2;L=#3;},line join=round]%
    % defining a few constants ~~~~~~~~~~
    \def\lpa{{180-goc}}% left polar angle
    \def\rpa{{360-goc}}% right polar angle
    \def\vec{(L,0)}% shift vector; replaced l by L to avoid mistake with 1
    
    % defining coordinates ~~~~~~~~~~~
    \path (\lpa:r) coordinate (A)%
        (\rpa:r) coordinate (B)%
        ({362-goc}:r) coordinate (Bt)%
        ([shift={\vec}]A) coordinate (D)%
        ([shift={\vec}]B) coordinate (C)%
        ([shift={\vec}]Bt) coordinate (Ct)%
        (270:r) coordinate (E)%
        ([shift={\vec}]E) coordinate (F)%
        ;

    % left solid part from back ~~~~~~~~~~~~
    \begin{scope}%
        \clip (A)--(Bt)--(Ct)--(D)--cycle;%
        \draw[shift={\vec}] (\lpa:r) arc (\lpa:{270}:r);%
    \end{scope}%

    % providing the dashed circular part ~~~~~~~~
    \begin{scope}%
        \clip (E)--(B)--(C)--(F)--cycle;%
        \draw[densely dashed, shift={\vec}] (\lpa:r) arc (\lpa:{270}:r);%
    \end{scope}%

    %right solid circle from back ~~~~~~~~~~~~~
    \draw[shift={\vec}] (270:r) arc ({270}:\rpa:r);%

    % all parts from front ~~~~~~~~~~~
    \draw (\lpa:r) arc (\lpa:\rpa:r)%
        (A)--(B)--(C)--(D)--cycle%
        (E)--(F);%
  \end{tikzpicture}%
}

\begin{document}
    goc / r / L
    

    13 / 1.5 / 5 \halfcyl{13}{1.5}{5}

    36 / 3 / 2  \halfcyl{36}{3}{2}

    50 / 2 / 4  \halfcyl{50}{2}{4}

    -10 / 2 / 4 \halfcyl{-10}{2}{4}

\end{document}

答案4

此代码可以自动绘制倾斜角度。但它不是空间中的正交投影。

\documentclass[border=3pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[declare function={goc=13;r=1.5;l=5;},line join=round]
\path ({180-goc}:r) coordinate (A)
({360-goc}:r) coordinate (B)
({362-goc}:r) coordinate (Bt)
([shift={(l,0)}]A) coordinate (D)
([shift={(l,0)}]B) coordinate (C)
([shift={(l,0)}]Bt) coordinate (Ct)
(270:r) coordinate (E)
([shift={(l,0)}]E) coordinate (F)
;
\begin{scope}
\clip (A)--(Bt)--(Ct)--(D)--cycle;
\draw[shift={(l,0)}] ({180-goc}:r) arc ({180-goc}:{270}:r);
\end{scope}
\begin{scope}
\clip (E)--(B)--(C)--(F)--cycle;
\draw[densely dashed, shift={(l,0)}] ({180-goc}:r) arc ({180-goc}:{270}:r);
\end{scope}
\draw[shift={(l,0)}] (270:r) arc ({270}:{360-goc}:r);
\draw ({180-goc}:r) arc ({180-goc}:{360-goc}:r)
(A)--(B)--(C)--(D)--cycle
(E)--(F);
\end{tikzpicture}
\end{document}

相关内容