使用 tikz 绘制封闭曲线路径

使用 tikz 绘制封闭曲线路径

如何使用 tikz 绘制任意闭合曲线路径?我发现很难控制 tikz 中的曲线。

有人能给出一个绘制下图的示例代码吗?

在此处输入图片描述

答案1

要绘制闭合曲线,我建议\draw plot [smooth cycle] coordinates {(x1,y1), (x2,y1), ..., (xn,yn)}。您可以调整坐标的数量和位置,以及tension\contour宏的定义和说明在此解决方案

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta, decorations.markings, calc, intersections}
\tikzset{arrowmark/.style={postaction={decorate, decoration={markings, mark=at position #1 with {\arrow{>};}}}},
    arrowmark/.default={.5}
}

\newcommand{\conw}{.1}
\newcommand{\contour}[3]{\coordinate (uu) at ($(#1)!1cm!(#2)-(#1)$); % unit vector from e1 to p1
    \path[name path=line2] let \p0=(uu), \p1=(#1), \p2=(#2) in
        (\x1+\y0*\conw,\y1-\x0*\conw)--(\x2+\y0*\conw,\y2-\x0*\conw);
    \path[name path=line1] let \p0=(uu), \p1=(#1), \p2=(#2) in
        (\x1-\y0*\conw,\y1+\x0*\conw)--(\x2-\y0*\conw,\y2+\x0*\conw);
    \path[name intersections={of=line1 and mainpath, by={i1}},
        name intersections={of=line1 and #3, by={i2}},
        name intersections={of=line2 and #3, by={i3}},
        name intersections={of=line2 and mainpath, by={i4}}];
    \draw[line width=2pt, white](i1)--(i4);
    \draw[line width=2pt, white](i2)--(i3);
    \begin{scope}[decoration={markings, mark=at position 0.5 with {\arrow{>}}}]
    \draw[line cap=round, postaction={decorate}](i1)--(i2);
    \draw[line cap=round, postaction={decorate}](i3)--(i4);
    \end{scope}
    }

\begin{document}

\begin{tikzpicture}
\draw[thick] (-1,0)--(4,0)(0,-.5)--(0,3);
\draw[name path=mainpath] plot [smooth cycle, tension=.8] 
    coordinates {(-.5,1.5)(.5,.5)(1.5,-.5)(3,0)(4,2)(2,3)}
    [arrowmark=.6];
\node(p1) at (2,1.5){$\times$}; 
\draw[name path=ellipse1, rotate=45, xscale=-1, arrowmark=.8](p1)circle[x radius=.6, y radius=.4];          
\end{tikzpicture}
\qquad
\begin{tikzpicture}
\draw[thick] (-1,0)--(4,0)(0,-.5)--(0,3);
\draw[name path=mainpath] plot [smooth cycle, tension=.8] 
    coordinates {(-.5,1.5)(.5,.5)(1.5,-.5)(3,0)(4,2)(2,3)}
    [arrowmark=.6];
\node(p1) at (2,1.5){$\times$}; 
\draw[name path=ellipse1, rotate=45, xscale=-1, arrowmark=.8](p1)circle[x radius=.6, y radius=.4]; 
\contour{5,5}{p1}{ellipse1};          
\end{tikzpicture}

\end{document}

相关内容