如何简单画出近乎任意的曲线?例如,如何绘制此图形:
来源 算法导论(第 3 版)作者:Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest、Clifford Stein | 第 45 页 | 图 3.1
现在我对如何绘制这样的图形有两个想法:
将每条曲线分成许多较小的曲线,这些较小的曲线可以用 Ti 绘制钾例如 Z 命令
\draw (0,0) to [in=80, out=-30] (3,2);
这种方式很自然。但是,用这种方式划分(例如上图中的线条)很困难。而且,当我已经划分好时,我很可能会对坐标感到困惑。
找到每条曲线的公式,然后画出曲线的图形。
这种方式对于一般的图形来说还好,但是对于像上图这种复杂的曲线来说,似乎不太可行。
因此,上述方法都不够好。您有什么绘制此类图形的方法吗?非常感谢您的帮助!
笔记
如果问题是重复的,我很抱歉。我搜索了一段时间,但没有成功。
如果你能帮我画出上面的图形,那将是我的荣幸。不过,我强烈希望有一个通用的解决方案,因为这个问题的目的不仅仅是问如何画出这个图形。
您可以看到没有 MWE在我的问题中。正如我上面解释的那样,到目前为止,我的所有想法都很糟糕,所以我没有应用其中任何一个。
我非常喜欢使用 Ti钾Z。但是,使用 PGF、PStricks 等的答案对我来说都非常有用。
答案1
这是试图将一些关键方法收集到一页上。显然,这个讨论并不详尽,所以我希望这篇文章能得到其他方法和/或意见的补充。(我没有努力精确地复制你的曲线,抱歉。)
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{multicol}
\usepackage{tikz}
\usetikzlibrary{hobby}
\begin{document}
\section*{A few ideas to draw smooth curves}
\begin{multicols}{2}
\subsection*{Your idea: use \texttt{out} and \texttt{in}}
This works, and the result looks smooth if you make sure that the \texttt{in}
of one point and the \texttt{out} of the next point differ by 180. In the
following pic, we have \texttt{\dots to[out=50,in=\textbf{210}] (1,2) circle(1pt)
to[\textbf{out=30},in=240] \dots} to illustrate this. Notice that the
\texttt{looseness} key can be of great help here.
\begin{tikzpicture}
\draw[latex-latex] (0,4) |- (6,0);
\draw[blue] (0,0) circle(1pt) to[out=50,in=210] (1,2) circle(1pt)
to[out=30,in=240,looseness=0.5] (3,3) circle(1pt);
\end{tikzpicture}
\subsection*{Your idea: find an analytic formula}
This is IMHO often the simplest way. One only needs to keep in mind a few basic
functions like $\sin$ and $\tanh$.
\begin{tikzpicture}
\draw[latex-latex] (0,4) |- (6,0);
\draw[blue] plot[variable=\x,domain=0:6,smooth]
({\x},{1+0.3*(tanh(3-\x)+1)*sin(180*\x)+0.2*\x});
\end{tikzpicture}
\subsection*{Use \texttt{plot[smooth] coordinates}}
A very convenient option, mentioned by Torj{\o}rn T.\ in the comments, is to use
\texttt{plot[smooth] coordinates}. You may want to play with the
\texttt{tension} key.
\begin{tikzpicture}
\draw[latex-latex] (0,4) |- (6,0);
\draw[blue] plot[smooth] coordinates
{(0,0) (1,2) (2,2) (3,3) (4,3)};
\draw[red] plot[smooth,tension=2] coordinates
{(0,0) (1,2) (2,2) (3,3) (4,3)};
\end{tikzpicture}
\subsection*{Use the \texttt{hobby} library}
The \texttt{hobby} library is sort of an el Dorado for smooth curve
constructors. It has way too many options to be listed here. One thing I find
very useful is the \texttt{tangent} style from the manual.
\begin{tikzpicture}[tangent/.style={%
in angle={(180+#1)},Hobby finish ,
designated Hobby path=next , out angle=#1,
}]
\draw[latex-latex] (0,4) |- (6,0);
\draw[blue] plot[smooth,hobby,tension=0.3] coordinates
{(0,0) (1,1.2) (2,2) (3,3) (4,3)};
\draw [red,use Hobby shortcut]
(0,0) .. ([tangent=30]1.5,1) .. ([tangent=-10]3,2) .. (5,1);
\end{tikzpicture}
I'd also like to mention that, when it comes to decorations, in my experience
the Hobby paths are advantageous. They often do not lead to \texttt{dimension too
large} errors when their almost identically looking \texttt{plot[smooth]}
counterparts do.
\subsection*{Not sure about \texttt{controls}}
There is also the possibility of using Bezier curves (without \texttt{hobby}).
Many users are able to use that to obtain great results. Unfortunately, I am not
one of those since IMHO the relation between position of the control points and
the outcome is not too obvious. But this is of course a very subjective
statement.
\end{multicols}
\end{document}
答案2
按照 marmot 的建议,可以使用贝塞尔曲线。对于这样的事情,我使用Tikz编辑,一款非常有用的 Ti 图表翻译软件钾Z. 我导入图表\node{\includegraphics{YourDiagram}};
并使用 TikzEdt 的贝塞尔工具精确复制图表。例如,对于您的第一个图,我得到以下结果:
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
%\node{\includegraphics[width=2\textwidth]{YourDiagram}};%import the diagram that you want to copy in TikzEdt
%\draw [help lines, step=.1cm] (-22.8,-7.4) grid (-7.7,8.2);
\draw (-21,-1.8) .. controls (-19.9,-1.8)
and (-19.7,-1.6) .. (-19.3,-2) .. controls (-18.5,-2.8)
and (-18.5,-1.7) .. (-18,-0.9) .. controls (-16.9,0.4)
and (-15.6,0.6) .. (-14.2,1.2) .. controls (-13,1.6)
and (-11.9,1.9) .. (-10.2,3.3) node[above]{{\LARGE$f(n)$}};
\draw(-21,-3.8) .. controls (-20.3,-3.1)
and (-20,-2.8) .. (-19.7,-2.4) .. controls (-18.9,-1.4)
and (-17.9,-1.4) .. (-17.6,-1.4) .. controls (-16.2,-1.4)
and (-16.1,-1.1) .. (-15.8,-0.9) .. controls (-15.1,-0.2)
and (-14.8,0.1) .. (-13.4,0.5) .. controls (-12.5,0.7)
and (-12,0.8) .. (-10.2,1.1) node[above]{{\LARGE$c_1g(n)$}};
\draw(-21,-3.8) .. controls (-20.7,-3.2)
and (-20.4,-2.7) .. (-19.8,-1.2) .. controls (-19.1,0.4)
and (-18.5,1.2) .. (-17,1.1) .. controls (-16.4,1.1)
and (-16.1,1.5) .. (-15.5,2.5) .. controls (-15.1,3.3)
and (-14.6,4.1) .. (-13.5,4.7) .. controls (-12.7,5.1)
and (-12.3,5.3) .. (-10.2,6.1) node[above]{{\LARGE$c_2g(n)$}};
\draw (-21,7.1) -- (-21,-3.8) -- (-10.1,-3.8) node[right]{{\LARGE$n$}};
\draw[dashed] (-18.3,0.8) -- (-18.3,-3.8) node[below] {\LARGE{$n_0$}};
\end{tikzpicture}
\end{document}
答案3
在给定的图像上放置一个坐标系并选择一些点:
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\begin{document}
\begin{pspicture}(-0.5,-0.5)(6.4,6.4)
\psaxes[labels=none,ticks=none]{->}(6,6)[$n$,0][,0]
\psxTick(1.8){n_0}
\pscurve[linecolor=red,linewidth=1.5pt]%
(0,1.6)(0.4,1.1)(0.6,0.9)(0.9,1.2)(1,1.8)(1.4,3)(1.8,2.5)(2,2)(2.3,1.9)(3,2.2)(4.5,3)(6,3.4)
\pscurve[linecolor=blue,linewidth=1.5pt](0,1)(1,1.8)(1.8,2.5)(2.5,3)(3.5,3.4)(4.5,4)(6,5)
\psline[linestyle=dashed](1.8,0)(1.8,2.5)
\rput[r](6,5.2){\textcolor{blue}{$cg(n)$}}
\rput[r](6,3.5){\textcolor{red}{$f(n)$}}
\end{pspicture}
\end{document}