我正在尝试绘制一个特殊且矛盾的情节:一个带有随机扭曲的圆圈,无论我构建文档多少次,它都必须保持不变。
下面是我想要实现的一个示例
该图由以下代码生成
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
% axes
\draw[ultra thin] (-2.5, 0) -- (2.5, 0);
\draw[ultra thin] (0, -2.5) -- (0, 2.5);
% undistorted sphere
\draw[ultra thin, dashed] (0, 0) circle (2.25cm);
\draw[ultra thin, dashed, ->] (0:0) -- (125:2.25);
\node at (-1, 1) {$R$};
% distrorted sphere
\draw[very thick] plot[domain=0:350,smooth cycle ] (\x:2+rnd*0.5);
\draw[very thick, ->] (0:0) -- (45:2.1);
\draw[ultra thin, ->] (1, 0) arc (0:45:1);
\node at (1.2, 0.4) {$\theta$};
\node at (0.7, 1.25) {$R(\theta)$};
\end{tikzpicture}
\end{document}
代码的问题在于,每当我对其进行更改并重建文档时,“扭曲”的圆圈都会具有不同的形状,因为我正在使用该rnd
函数。我想要一个恒定随机形状的原因是因为我想以角度 θ 表示其半径,因此它必须接触圆的轮廓。
有什么想法可以实现这样的事情吗?
答案1
用户回答JPG,矢量R
不与随机曲线相交。为了实现两条路径的相交,我们必须使用库intersections
。添加到 JPG 的答案:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,intersections}
\begin{document}
\begin{tikzpicture}[>=latex]
\pgfmathsetseed{1} % choose a number which give a good shape to your circle
% axes
\draw[ultra thin] (-2.5, 0) -- (2.5, 0);
\draw[ultra thin] (0, -2.5) -- (0, 2.5);
% undistorted sphere
\draw[ultra thin, dashed] (0, 0) circle (2.25cm);
\draw[ultra thin, dashed, ->] (0:0) -- (125:2.25);
\node at (-1, 1) {$R$};
% distrorted sphere
\path[draw,very thick, name path=curve] plot[domain=0:350,smooth cycle ] (\x:2+rnd*0.5);
\path[very thick, name path=line] (0:0) -- (45:2.5);
\draw [name intersections={of=curve and line, by=x}];
\draw [very thick, ->] (0,0)--(x);
\draw[ultra thin, ->] (1, 0) arc (0:45:1);
\node at (1.2, 0.4) {$\theta$};
\node at (0.7, 1.25) {$R(\theta)$};
\end{tikzpicture}
\end{document}
答案2
用于\pgfmathsetseed
初始化随机数生成的种子:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\pgfmathsetseed{1} % choose a number which give a good shape to your circle
% axes
\draw[ultra thin] (-2.5, 0) -- (2.5, 0);
\draw[ultra thin] (0, -2.5) -- (0, 2.5);
% undistorted sphere
\draw[ultra thin, dashed] (0, 0) circle (2.25cm);
\draw[ultra thin, dashed, ->] (0:0) -- (125:2.25);
\node at (-1, 1) {$R$};
% distrorted sphere
\draw[very thick] plot[domain=0:350,smooth cycle ] (\x:2+rnd*0.5);
\draw[very thick, ->] (0:0) -- (45:2.1);
\draw[ultra thin, ->] (1, 0) arc (0:45:1);
\node at (1.2, 0.4) {$\theta$};
\node at (0.7, 1.25) {$R(\theta)$};
\end{tikzpicture}
\end{document}
编辑:如果你想自动计算箭头的长度
然后,您可以在绘图之前生成箭头的长度,并使用此长度来获得所需的精确角度。由于您不知道绘图将以哪些角度计算,因此您应该从要绘制箭头的角度开始绘图:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\pgfmathsetseed{1}
% axes
\draw[ultra thin] (-2.5, 0) -- (2.5, 0);
\draw[ultra thin] (0, -2.5) -- (0, 2.5);
% undistorted sphere
\draw[ultra thin, dashed] (0, 0) circle (2.25cm);
\draw[ultra thin, dashed, ->] (0:0) -- (125:2.25);
\node at (-1, 1) {$R$};
% distrorted sphere
\pgfmathsetmacro\RR{2+rnd*0.5}
\draw[very thick] plot[domain=45:395,smooth cycle ] (\x:{ifthenelse(\x==45,\RR,2+rnd*0.5)});
\draw[very thick, ->] (0:0) -- (45:\RR);
\draw[ultra thin, ->] (1, 0) arc (0:45:1);
\node at (1.2, 0.4) {$\theta$};
\node at (0.7, 1.25) {$R(\theta)$};
\end{tikzpicture}
\end{document}