![TikZ-decoration:沿曲線控制裝飾幅度](https://rvso.com/image/392325/TikZ-decoration%EF%BC%9A%E6%B2%BF%E6%9B%B2%E7%B7%9A%E6%8E%A7%E5%88%B6%E8%A3%9D%E9%A3%BE%E5%B9%85%E5%BA%A6.png)
使用基於的路徑裝飾這個解決方案由土撥鼠提供,我正在尋找改變圖形上裝飾幅度的可能性。
有這個情節
應用上述裝飾給出
這正是裝飾該做的事。
事實上,所需的曲線應該如下所示:
最後的輸出是透過手動搜尋正確的位置來操縱幅度創建的,這是一種「試錯」方法。改變尺寸tikzpicture
將會給出錯誤的結果 fx
現在的基本想法是提供一個單獨的路徑(可以在開發過程中使其可見)來控制沿著原始(藍色)曲線的裝飾幅度。在這種情況下,控制路徑(紅色)將非常簡單:
控制路徑可以解釋為可通過 設定的裝飾幅度的一個因素decoration={amplitude=}
。
假設這種方法會非常方便,我有點驚訝它不可用TikZ
- 或者我是否監督過它?如果不是:如何獲得\state{step}
裝飾定義部分內控製曲線的 y 值?
MWE 產生上述所有圖表(即使在效率和結構美方面沒有很好地編碼):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing}
\newcounter{randymark}
\newcommand{\amplitudesetter}{}
\pgfdeclaredecoration{mark random y steps}{start}
{%
\state{start}[width=+0pt,next state=step,persistent precomputation={\pgfdecoratepathhascornerstrue\setcounter{randymark}{0}}]
{\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{0pt}{0pt}}
}%
\state{step}[auto end on length=1.5\pgfdecorationsegmentlength,
auto corner on length=1.5\pgfdecorationsegmentlength,
width=+\pgfdecorationsegmentlength]
{\stepcounter{randymark}\amplitudesetter
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{\pgfdecorationsegmentlength}{rand*\pgfdecorationsegmentamplitude}}
}%
\state{final}
{\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpointdecoratedpathlast}%
}%
}%
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={mark random y steps,segment length=1.5mm,amplitude=0.75mm}]% original curve
\draw[style=help lines] (0,-4) grid[step=5mm] (25,1);
\pgfmathsetseed{2}
\draw[blue!80!black,thick] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\end{tikzpicture}
\vspace{2ex}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={mark random y steps,segment length=1.5mm,amplitude=0.75mm}]% original curve
\draw[style=help lines] (0,-4) grid[step=5mm] (25,1);
\pgfmathsetseed{2}
\draw[black] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\path[decorate] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\draw[blue!80!black,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth] (randymark\x);
\end{tikzpicture}
\vspace{2ex}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={mark random y steps,segment length=1.5mm,amplitude=0.75mm}]% original curve
\draw[style=help lines] (0,-4) grid[step=5mm] (25,1);
\pgfmathsetseed{2}
\renewcommand{\amplitudesetter}{%
\pgfdecorationsegmentamplitude=0.75mm
\ifnum\value{randymark}<48\pgfdecorationsegmentamplitude=0.7mm\fi%
\ifnum\value{randymark}<46\pgfdecorationsegmentamplitude=0.6mm\fi%
\ifnum\value{randymark}<44\pgfdecorationsegmentamplitude=0.5mm\fi%
\ifnum\value{randymark}<42\pgfdecorationsegmentamplitude=0.4mm\fi%
\ifnum\value{randymark}<40\pgfdecorationsegmentamplitude=0.3mm\fi%
\ifnum\value{randymark}<38\pgfdecorationsegmentamplitude=0.2mm\fi%
\ifnum\value{randymark}<36\pgfdecorationsegmentamplitude=0.1mm\fi%
\ifnum\value{randymark}<34\pgfdecorationsegmentamplitude=0mm\fi%
\ifnum\value{randymark}<8\pgfdecorationsegmentamplitude=0.75mm\fi%
}
\draw[black] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\path[decorate] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\draw[blue!80!black,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth] (randymark\x);
\end{tikzpicture}
\vspace{2ex}
\begin{tikzpicture}[x=2.5mm,y=2.5mm,decoration={mark random y steps,segment length=1.5mm,amplitude=0.75mm}]% original curve
\draw[style=help lines] (0,-4) grid[step=5mm] (25,1);
\pgfmathsetseed{2}
\renewcommand{\amplitudesetter}{%
\pgfdecorationsegmentamplitude=0.75mm
\ifnum\value{randymark}<48\pgfdecorationsegmentamplitude=0.7mm\fi%
\ifnum\value{randymark}<46\pgfdecorationsegmentamplitude=0.6mm\fi%
\ifnum\value{randymark}<44\pgfdecorationsegmentamplitude=0.5mm\fi%
\ifnum\value{randymark}<42\pgfdecorationsegmentamplitude=0.4mm\fi%
\ifnum\value{randymark}<40\pgfdecorationsegmentamplitude=0.3mm\fi%
\ifnum\value{randymark}<38\pgfdecorationsegmentamplitude=0.2mm\fi%
\ifnum\value{randymark}<36\pgfdecorationsegmentamplitude=0.1mm\fi%
\ifnum\value{randymark}<34\pgfdecorationsegmentamplitude=0mm\fi%
\ifnum\value{randymark}<8\pgfdecorationsegmentamplitude=0.75mm\fi%
}
\draw[black] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\path[decorate] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\draw[blue!80!black,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth] (randymark\x);
\end{tikzpicture}
\vspace{2ex}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={mark random y steps,segment length=1.5mm,amplitude=0.75mm}]
\draw[style=help lines] (0,-4) grid[step=5mm] (25,1);
\pgfmathsetseed{2}
\draw [red,thick,name=amplitudecontrol] (0,1) -- (2,1) -- (2,0) -- (7,0) -- (12,1) -- (25,1);
\draw[blue!80!black,thick] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\end{tikzpicture}
\end{document}
答案1
首先我要說的是,我真的很喜歡這個問題,並且對你所取得的成就印象深刻。這是解決可擴展性的提案。定義一個控制幅度的函數,
varyingamp(x) = whatever you like
其中x
是修飾路徑的分數(為了確保可擴展性)。 (這樣的功能已經被使用過這裡為了具有可變的線寬。如果我以前使用過類似的東西,我一點也不會感到驚訝。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing}
\newcounter{randymark}
%\newcommand{\amplitudesetter}{}
\pgfdeclaredecoration{mark random y steps}{start}
{%
\state{start}[width=+0pt,next state=step,persistent precomputation={
\pgfdecoratepathhascornerstrue\setcounter{randymark}{0}}]
{\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{0pt}{0pt}}
}%
\state{step}[auto end on length=1.5\pgfdecorationsegmentlength,
auto corner on length=1.5\pgfdecorationsegmentlength,
width=+\pgfdecorationsegmentlength]
{\stepcounter{randymark}%\amplitudesetter
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{\pgfdecorationsegmentlength}{rand*\pgfdecorationsegmentamplitude}}
}%
\state{final}
{\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpointdecoratedpathlast}%
}%
}%
\pgfdeclaredecoration{mark varying random y steps}{start}
{%
\state{start}[width=+0pt,next state=step,persistent precomputation={
\pgfdecoratepathhascornerstrue\setcounter{randymark}{0}}]
{\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{0pt}{0pt}}
}%
\state{step}[auto end on length=1.5\pgfdecorationsegmentlength,
auto corner on length=1.5\pgfdecorationsegmentlength,
width=+\pgfdecorationsegmentlength]
{\stepcounter{randymark}
\pgfmathsetmacro{\myfraction}{\the\pgfdecorationsegmentlength*\value{randymark}/\pgfdecoratedpathlength}
\pgfmathsetmacro{\myamplitude}{varyingamp(\myfraction)}
%\typeout{\myfraction,\myamplitude}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{\pgfdecorationsegmentlength}{rand*\myamplitude*\pgfdecorationsegmentamplitude}}
}%
\state{final}
{\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpointdecoratedpathlast}%
}%
}%
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={mark random y steps,segment length=1.5mm,amplitude=0.75mm}]% original curve
\draw[style=help lines] (0,-4) grid[step=5mm] (25,1);
\pgfmathsetseed{2}
\draw[blue!80!black,thick] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\end{tikzpicture}
\vspace{2ex}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={mark varying random y steps,segment
length=1.5mm,amplitude=0.75mm},declare function={
varyingamp(\x)=ifthenelse(\x<0.08,1,ifthenelse(\x<0.28,0,ifthenelse(\x<0.48,5*(\x-0.28),1)));}]%
\draw[style=help lines] (0,-4) grid[step=5mm] (25,1);
\draw[red,thick] plot[variable=\x,domain=0:25,samples=101] ({\x},{varyingamp(\x/25)});
\pgfmathsetseed{2}
\draw[black] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\path[decorate] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\draw[blue!80!black,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth] (randymark\x);
\end{tikzpicture}
\vspace{2ex}
\begin{tikzpicture}[x=2.5mm,y=2.5mm,decoration={mark varying random y
steps,segment length=0.75mm,amplitude=0.75mm},declare function={
varyingamp(\x)=ifthenelse(\x<0.08,1,ifthenelse(\x<0.28,0,ifthenelse(\x<0.48,5*(\x-0.28),1)));}]
\draw[style=help lines] (0,-4) grid[step=5mm] (25,1);
\pgfmathsetseed{2}
\draw[black] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\path[decorate] (0,0) -- (2,0) to [out=0,in=180](4,-3.5) to [out=0,in=225](6,-1.75) to [out=45,in=180](11,0) -- (24.25,0);
\draw[blue!80!black,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth] (randymark\x);
\end{tikzpicture}
\end{document}
函數在第二張圖中以紅色顯示。第三張圖顯示了可擴展性。 (當然,您還需要重新調整段長度。另請注意,此裝飾具有離散步驟,因此如果您有一個強烈變化的函數但只有幾個步驟,則該函數可能不會被完全“欣賞”,因為它只被評估在幾個點上。