如何在 TikZ 中绘制一条沿连接两个任意节点的对角线延伸的美观的弯曲随机线?
(此外,我该如何像平常一样添加箭头,还是我必须在某个点创建一个箭头并将其叠加在端点上?)
(本主题中有一些关于如何制作好看的垂直线或水平线的想法在技术图纸上随意绘制参差不齐的线条。请从那里使用 MWE。
\documentclass[12pt]{article} % SIZE OF FONT AND ITS LAYOUT ON EACH PAGE.
\usepackage[top = 1in, bottom = 1in, left = 1in, right = 1in]{geometry}
\usepackage{amsmath, booktabs, graphicx, setspace}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,tkz-graph,tikz-cd} % DIAGRAMS.
\usetikzlibrary{arrows, calc, decorations.markings}
\usetikzlibrary{calc}
\newcommand\irregularline[2]{%
let \n1 = {(rand*(#1)} in
+(0,\n1)
\foreach \a in {0.1,0.2,...,#2}{
let \n1 = {rand*(#1)} in
-- +(\a,\n1)
}} % #1=seed, #2=length of horizontal line
% This is code for a horizontal random line that I like.
% How to make this sort of line connect two arbitrary points,
% and perhaps, how to make it curve in a natural way?
\begin{document}
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}[scale=1.8,auto]
% replace with randomly ragged line that curves
\draw [-,line width=1pt] (0,0) edge (3,3);
\end{tikzpicture}
\end{center}
\caption{CAPTION GOES HERE}
\label{fig:FIGURE NAME GOES HERE}
\end{figure}
\end{document}
(使用如何绘制不规则的圆形(形状)?,而这个,我正在尝试制作相图而不是手工绘制。)
答案1
这是使用 Tikz(而不是 pgf)的另一个分形解决方案。
我定义了一个用两个参数to path
命名的新样式:fractal line
移动每段中间的长度比。
应用循环的最小长度。
(0,0)
在和之间绘制分形线的使用示例(5,0)
:
\draw (0,0) to[fractal line=.1 and 1mm] (5,0);
完整代码:
\documentclass[convert={size=480},margin=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
fractal line/.style args={#1 and #2}{%
% #1 is ratio of length to move the middle of each segment
% #2 is the minimum length to apply the recurrence
to path={
let
\p1=(\tikztostart), % start point
\p2=(\tikztotarget), % end point
\n1={veclen(\x1-\x2,\y1-\y2)}, % distance
\p3=($(\p1)!.5!(\p2)$), % middle point
\p4=(rand*#1*\n1,rand*#1*\n1), % random vector
\p5=(\x3+\x4,\y3+\y4) % random moved middle point
in \pgfextra{
\pgfmathtruncatemacro\mytest{(\n1<#2)?1:0}
\ifnum\mytest=1 %
\tikzset{fractal line/.style args={#1 and #2}{line to}}
\fi
} to[fractal line=#1 and #2] (\p5) to[fractal line=#1 and #2] (\p2)
},
},
}
\begin{document}
\begin{tikzpicture}
\draw[rounded corners=1mm,-stealth] (0,0) to[fractal line=.05 and 1cm] (10,0);
\draw[rounded corners=.3mm,-stealth] (0,0) to[fractal line=.2 and 3mm] (4,4);
\end{tikzpicture}
\end{document}
答案2
回答你最初的问题,下面画了一条连接两点的分形路径。在本例中,从 (0,0) 到 (3,3)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
% zero mean Gaussian random number with variance=1
\newcount\gaussF
\edef\gaussR{0}
\edef\gaussA{0}
\makeatletter
\pgfmathdeclarefunction{randgauss}{0}{%
\global\advance\gaussF by\@ne
\ifodd\gaussF
\pgfmathrnd@%
\pgfmathadd@{.0000001}{\pgfmathresult}%
\pgfmathln@{\pgfmathresult}%
\pgfmathmultiply@{-2}{\pgfmathresult}%
\pgfmathsqrt@{\pgfmathresult}%
\global\let\gaussR=\pgfmathresult%radius = $sqrt(-2*ln(rnd))$
\pgfmathrnd@%
\pgfmathmultiply@{360}{\pgfmathresult}%
\global\let\gaussA=\pgfmathresult%angle = $360*rnd$
\pgfmathcos@{\pgfmathresult}%
\pgfmathmultiply@{\pgfmathresult}{\gaussR}%
\else
\pgfmathsin@{\gaussA}%
\pgfmathmultiply@{\gaussR}{\pgfmathresult}%
\fi
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{fracI}
\newcount\fracN
\edef\fracF{0.5}% Controls shape of fractal. Must be < 1.
\edef\fracNPT{64}% number of points to plot
\begin{document}
\begin{center}
\makeatletter
\begin{tikzpicture}
\pgfmathadd{1}{-\fracF}%
\global\let\fracS = \pgfmathresult%
\global\fracN = \fracNPT
\global\divide\fracN by\tw@
\coordinate (frac0) at (0,0);% starting point
\coordinate (frac\fracNPT) at (3,3);% ending point
% calculate perpendicular. Can replace 1cm with scale factor.
\coordinate (fracP) at ($(frac0)!1cm!90:(frac\fracNPT) - (frac0)$);
\global\c@fracI = \z@
\edef\fracR{frac0}
\loop\ifnum\fracN > \z@
\ifnum\c@fracI < \fracNPT\relax
\global\let\fracL = \fracR%
\global\advance\c@fracI by \fracN
\edef\fracM{frac\arabic{fracI}}
\global\advance\c@fracI by \fracN
\edef\fracR{frac\arabic{fracI}}
\pgfmathmultiply{\fracS}{randgauss}%
\global\let\fracY = \pgfmathresult%
\coordinate (\fracM) at ($(\fracL)!0.5!(\fracR) + \fracY*(fracP)$);
\else
\global\divide\fracN by\tw@
\pgfmathmultiply{\fracS}{\fracF}%
\global\let\fracS = \pgfmathresult%
\global\c@fracI = \z@
\edef\fracR{frac0}
\fi
\repeat
% now draw line
\setcounter{fracI}{0}
\edef\fracR{frac0}
\loop\ifnum\c@fracI < \fracNPT\relax
\stepcounter{fracI}
\global\let\fracL = \fracR
\edef\fracR{frac\arabic{fracI}}
\draw (\fracL) -- (\fracR);
\repeat
\end{tikzpicture}
\makeatother
\end{center}
\end{document}