TikZ — 滚动对数螺旋

TikZ — 滚动对数螺旋

我几乎没有时间看这张图片,所以代码很乱,可能是 TikZ 中永远不要这样做的代码的最好例子,但它确实有效:

结果图像

\documentclass[12pt, border=0.5mm]{standalone}
\usepackage{graphicx}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}    
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}

\begin{document}

\begin{tikzpicture}[x=0.75mm, y=0.75mm]

    \clip[] (-95, -26) rectangle (30, 12);

    \def\k{7};          

    \begin{scope}[rotate={deg(0.5*pi) - atan(1/0.22) - deg(0.03*pi)}]   

        \draw[red, line cap=round, line width=0.2mm, domain=-5*pi:2*pi, variable=\t, samples=500] plot[fixed point arithmetic] ({\t r}:{\k*exp(0.22*\t)});

        \draw[line width=0.1mm, domain=-91:0, variable=\t] plot[fixed point arithmetic] (\t, {tan(deg(1.03*pi))*\t});

        \draw[line width=0.1mm] ({0.53*pi r}:{\k*exp(0.22*0.53*pi)}) -- ({1.53*pi r}:{\k*exp(0.22*1.53*pi)});
        \draw[line width=0.1mm] ({1.03*pi r}:{\k*exp(0.22*1.03*pi)}) -- ({0.03*pi r}:{\k*exp(0.22*0.03*pi)});

        \begin{scope}
            \clip[rotate={deg(0.53*pi) + atan(1/0.22)}] (20, 0) rectangle (65, 19.75);  
            \clip[rotate={deg(0.03*pi)}] (-20, 0) rectangle (-65, -20); 
            \draw[line width=0.1mm] ({-(\k*exp(0.22*1.53*pi) - \k*exp(0.22*1.03*pi))/sin(atan(0.22)}, 0) -- ({\k*exp(0.22*1.53*pi)*cos(deg(1.53*pi))-(\k*exp(0.22*1.53*pi) - \k*exp(0.22*1.03*pi))/sin(atan(0.22)}, {\k*exp(0.22*1.53*pi)*sin(deg(1.53*pi))});
            \draw[line width=0.1mm] ({(-(\k*exp(0.22*1.53*pi) - \k*exp(0.22*1.03*pi))/sin(atan(0.22))-(\k*exp(0.22*1.03*pi) - \k*exp(0.22*0.53*pi))/sin(atan(0.22)}, 0) -- 
                ({\k*exp(0.22*1.53*pi)*cos(deg(1.53*pi))-(\k*exp(0.22*1.53*pi) - \k*exp(0.22*1.03*pi))/sin(atan(0.22))-(\k*exp(0.22*1.03*pi) - \k*exp(0.22*0.53*pi))/sin(atan(0.22)}, {\k*exp(0.22*1.53*pi)*sin(deg(1.53*pi))});
            \draw[line width=0.1mm] ({((-(\k*exp(0.22*1.53*pi) - \k*exp(0.22*1.03*pi))/sin(atan(0.22))-(\k*exp(0.22*1.03*pi) - \k*exp(0.22*0.53*pi))/sin(atan(0.22))-(\k*exp(0.22*0.53*pi) - \k*exp(0.22*0.03*pi))/sin(atan(0.22)}, 0) -- ({\k*exp(0.22*1.53*pi)*cos(deg(1.53*pi))-(\k*exp(0.22*1.53*pi) - \k*exp(0.22*1.03*pi))/sin(atan(0.22))-(\k*exp(0.22*1.03*pi) - \k*exp(0.22*0.53*pi))/sin(atan(0.22))-(\k*exp(0.22*0.53*pi) - \k*exp(0.22*0.03*pi))/sin(atan(0.22)}, {\k*exp(0.22*1.53*pi)*sin(deg(1.53*pi))});
        \end{scope}

        \draw[line width=0.2mm, domain=-95:25, variable=\t] plot[fixed point arithmetic] (\t, {tan(deg(0.53*pi)+atan(1/0.22))*\t + \k*exp(0.22*1.53*pi)*sin(deg(1.53*pi)) - tan(deg(0.53*pi)+atan(1/0.22))*\k*exp(0.22*1.53*pi)*cos(deg(1.53*pi))});

        \draw[line width=0.1mm, domain={deg(0.53*pi)}:{deg(0.53*pi)+atan(1/0.22)}, samples=100] plot({\k*exp(0.22*1.53*pi)*cos(deg(1.53*pi)) + 5*cos(\x)}, {\k*exp(0.22*1.53*pi)*sin(deg(1.53*pi)) + 5*sin(\x)});
                \node[font=\tiny] at (-0.35, -18) {$\gamma_0$};

        \node[font=\scriptsize] at (4, -10) {$R$};
        \node[font=\scriptsize] at (2, -23) {$P$};                              
        \node[font=\scriptsize] at (-52, -17) {$s(R)$};                             

        \fill[] (0, 0) circle (0.2mm);  

    \end{scope}
\end{tikzpicture}

\end{document}

我正在重新绘制这张(手绘!)图片:

在此处输入图片描述

我的问题是,如何做才是正确的?代码可以工作,但没有什么值得骄傲的。我将来会修复它,所以这篇文章主要是提醒我自己。

答案1

显然,这可以进一步简化:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic,angles,quotes}
\begin{document}
\begin{tikzpicture}[x=0.75mm, y=0.75mm, font=\scriptsize, line width=.1mm]
  \def\k{7}
  \pgfmathsetmacro\m{\k*exp(0.22*1.53*pi)}
  \pgfmathsetmacro\n{\k*exp(0.22*0.03*pi)}
  \pgfmathsetmacro\p{\k*exp(0.22*0.53*pi)}
  \pgfmathsetmacro\q{\k*exp(0.22*1.03*pi)}
  \begin{scope}[rotate={deg(0.5*pi) - atan(1/0.22) - deg(0.03*pi)}]
    \draw [red, line cap=round, line width=0.2mm, domain=-5*pi:2*pi, variable=\t, samples=500] coordinate (o) plot [fixed point arithmetic] ({\t r}:{\k*exp(0.22*\t)}) coordinate (a);
    \draw [domain=-91:0, variable=\t] plot[fixed point arithmetic] (\t, {tan(deg(1.03*pi))*\t});
    \draw  ({0.53*pi r}:{\p}) coordinate (b) -- ({1.53*pi r}:{\m}) coordinate (p) node [below] {$P$} node [pos=.65, right] {$R$};
    \draw  ({1.03*pi r}:{\q}) -- ({0.03*pi r}:{\n});
    \begin{scope}
      \clip[rotate={deg(0.53*pi) + atan(1/0.22)}] (20, 0) rectangle (65, 19.75);
      \clip[rotate={deg(0.03*pi)}] (-20, 0) rectangle (-65, -20);
      \draw  ({(\q - \m)/sin(atan(0.22)}, 0) -- ({\m*cos(deg(1.53*pi))-(\m - \q)/sin(atan(0.22)}, {\m*sin(deg(1.53*pi))});
      \draw  ({(\p - \m)/sin(atan(0.22))}, 0) --
      ({\m*cos(deg(1.53*pi))+(\p - \m)/sin(atan(0.22))}, {\m*sin(deg(1.53*pi))});
      \draw  ({(\n - \m)/sin(atan(0.22))}, 0) -- ({\m*cos(deg(1.53*pi))+(\n - \m)/sin(atan(0.22))}, {\m*sin(deg(1.53*pi))});
    \end{scope}
    \node at (-52, -17) {$s(R)$};
    \fill (0, 0) circle (0.2mm);
  \end{scope}
  \draw [line width=0.2mm] (a |- p) -- ++(-120,0) coordinate (b);
  \pic [draw, font=\tiny, "$\gamma_0$"] {angle=o--p--b};
\end{tikzpicture}
\end{document}

稍微简单一点的代码

答案2

因此,这里是没有旋转和剪辑技巧的代码:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}

\begin{document}
\begin{tikzpicture}[x=1mm, y=1mm]

\def\k{7} 
\def\a{0.22}

\pgfmathsetmacro\tAngle{atan(1/\a)}
\pgfmathsetmacro\tAngleRad{2*pi(\tAngle/360)}

\pgfmathsetmacro\rOne{\k*exp(\a*(\tAngleRad))}
\pgfmathsetmacro\rTwo{\k*exp(\a*(\tAngleRad+0.5*pi))}
\pgfmathsetmacro\rThree{\k*exp(\a*(\tAngleRad+pi))}
\pgfmathsetmacro\rFour{\k*exp(\a*(\tAngleRad+1.5*pi))}

\pgfmathsetmacro\lOne{(\rFour-\rThree)/sin(atan(\a))}
\pgfmathsetmacro\lTwo{(\rThree-\rTwo)/sin(atan(\a))+\lOne}
\pgfmathsetmacro\lThree{(\rTwo-\rOne)/sin(atan(\a))+\lTwo}

\pgfmathsetmacro\startAngle{-4*pi}
\pgfmathsetmacro\endAngle{2*pi}  

\foreach \step in {0, 0.5*pi, 1.5*pi} {
    \draw[line width=0.05mm] (0, 0) -- 
    ({\k*exp(\a*(\tAngleRad+\step))*cos(deg(\tAngleRad+\step))}, {\k*exp(\a*(\tAngleRad+\step))*sin(deg(\tAngleRad+\step))}); 
}

\pgfmathsetmacro\an{2*pi*(360-\tAngle)/360} 

\pgfmathsetmacro\Sx{(\k*exp(\a*\an)*sin(deg(\an)))/(tan(90-\tAngle) - tan(\tAngle+deg(\an)))}
\pgfmathsetmacro\Sy{\k*exp(\a*\an)*sin(deg(\an)) + tan(\tAngle+deg(\an))}

\draw [line cap=round, line width=0.1mm, domain=\Sx:0, variable=\x] plot [fixed point arithmetic] ({\x}, {tan(90-\tAngle)*\x}); 

\draw [line cap=round, line width=0.1mm, domain=-100:14, variable=\x] plot [fixed point arithmetic]
        ({\k*exp(\a*\an)*cos(\an r) + \x}, {\k*exp(\a*\an)*sin(\an r) + tan(\tAngle+(\an r))*\x});

\draw [line cap=round, line width=0.2mm, domain=\startAngle:\endAngle, variable=\angle, samples=5000] 
    plot [fixed point arithmetic] ({\k*exp(\a*\angle)*cos(\angle r)}, {\k*exp(\a*\angle)*sin(\angle r)});

\fill[] (\Sx, \Sy) circle (0.25mm);

\pgfmathsetmacro\Px{\k*exp(\a*(\tAngleRad+1.5*pi))*cos(deg(\tAngleRad+1.5*pi))}
\pgfmathsetmacro\Py{\k*exp(\a*(\tAngleRad+1.5*pi))*sin(deg(\tAngleRad+1.5*pi))}

\draw [line cap=butt, 
        line width=0.05mm,
        domain=180:{180-\tAngle}, 
        variable=\angle, 
        samples=100] plot [fixed point arithmetic]
    ({\Px + 6*cos(\angle)}, {\Py + 6*sin(\angle)}); 

\fill[] (\Px, \Py) circle (0.25mm);

\fill[] (\Px-\lOne, \Py) circle (0.25mm);
\fill[] (\Px-\lTwo, \Py) circle (0.25mm);
\fill[] (\Px-\lThree, \Py) circle (0.25mm);

\pgfmathsetmacro\xOne{(\Py - tan(-\tAngle)*(\Px-\lOne))/(tan(90-\tAngle) - tan(-\tAngle))}
\pgfmathsetmacro\xTwo{(\Py - tan(-\tAngle)*(\Px-\lTwo))/(tan(90-\tAngle) - tan(-\tAngle))}
\pgfmathsetmacro\xThree{(\Py - tan(-\tAngle)*(\Px-\lThree))/(tan(90-\tAngle) - tan(-\tAngle))}      

\draw [line cap=round, line width=0.1mm, domain=\xOne:{\Px-\lOne}, variable=\x] plot [fixed point arithmetic] 
    ({\x}, {tan(-\tAngle)*\x + \Py - tan(-\tAngle)*(\Px-\lOne)});           
\draw [line cap=round, line width=0.1mm, domain=\xTwo:{\Px-\lTwo}, variable=\x] plot [fixed point arithmetic]
    ({\x}, {tan(-\tAngle)*\x + \Py - tan(-\tAngle)*(\Px-\lTwo)});   
\draw [line cap=round, line width=0.1mm, domain=\xThree:{\Px-\lThree}, variable=\x] plot [fixed point arithmetic] 
    ({\x}, {tan(-\tAngle)*\x + \Py - tan(-\tAngle)*(\Px-\lThree)});

\fill[] (\xOne, {tan(90-\tAngle)*\xOne}) circle (0.25mm);
\fill[] (\xTwo, {tan(90-\tAngle)*\xTwo}) circle (0.25mm);
\fill[] (\xThree, {tan(90-\tAngle)*\xThree}) circle (0.25mm);

\fill[] (0, 0) circle (0.25mm);

\node[font=\scriptsize] at ({\Px + 3.5*cos(180-0.5*\tAngle)}, {\Py + 3.5*sin(180-0.5*\tAngle)}) {$\gamma_0$};
\node[font=\scriptsize, anchor=north] at ({\Px}, {\Py}) {$P$};
\node[font=\scriptsize, anchor=north] at ({\Px}, {\Py+12}) {$R$};
\node[font=\scriptsize, anchor=north] at ({-50}, {\Py}) {$s(R)$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容