![在 TikZ 中在等斜線上繪製斜率](https://rvso.com/image/399812/%E5%9C%A8%20TikZ%20%E4%B8%AD%E5%9C%A8%E7%AD%89%E6%96%9C%E7%B7%9A%E4%B8%8A%E7%B9%AA%E8%A3%BD%E6%96%9C%E7%8E%87.png)
我需要繪製 ODE 的等傾線($y' = x^2 - y^2$
在我的範例中)。我使用的程式碼來自這個問題(有一些小的變化)並且它運行得很好,但是我遇到了三個問題。
- 我無法在綠色等傾線上繪製斜率(函數
g5(x)
和g6(x)
程式碼中),因為x
當 時未定義變數-1 < x < 1
。是否可以\foreach
「跳過」一個間隔(0,1)
?我嘗試使用這樣的東西:\ifnum \xmin+\i*\hx < -1 draw...
但\ifnum
僅適用於整數。我也嘗試使用\breakforeach
但沒有成功。 - 是否可以將斜坡中心放在等斜線上?
(x0,y0)
設為斜坡的左端,(x1,y1)
為斜坡的右端。我想如果我進行翻譯,每個斜坡的中心將位於相應的等傾線上x0 -> x0 - abs(x0 - (x0+x1)/2), y0 -> y0 - abs(y0 - (y0+y1)/2)
。但當有atan2
斜坡的右端時,我不知道如何用 LaTeX 程式碼寫它。 - 左側綠色曲線上有一個小間隙。右側綠色曲線上沒有這樣的間隙。
微量元素
\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[declare function=
{f(\x,\y)=\x*\x-\y*\y;
g1(\x)=\x;
g2(\x)=-\x;
g3(\x)=sqrt(\x*\x+1);
g4(\x)=-sqrt(\x*\x+1);
g5(\x)=sqrt((\x*\x)-1);
g6(\x)=-sqrt((\x*\x)-1);
},
scale=2.5]
\def\xmax{2.0} \def\xmin{-2.0}
\def\ymax{2.0} \def\ymin{-2.0}
\def\nx{15} \def\ny{15}
\draw[red] plot[domain=\xmin-0.1:\xmax+0.1] (\x,{g1(\x)});
\draw[red] plot[domain=\xmin-0.1:\xmax+0.1] (\x,{g2(\x)});
\draw[red] plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
\draw[red] plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
\draw[green] plot[samples=1000, smooth,domain=\xmin-0.1:-1.0] (\x,{g5(\x)});
\draw[green] plot[samples=100, smooth,domain=1:\xmax+0.1] (\x,{g5(\x)});
\draw[green] plot[samples=1000, smooth,domain=\xmin-0.1:-1.0] (\x,{g6(\x)});
\draw[green] plot[samples=100, smooth,domain=1:\xmax+0.1] (\x,{g6(\x)});
\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
\draw[blue,-]
({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15);
\draw[blue,-]
({\xmin+\i*\hx},{g2(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15);
\draw[blue,-]
({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
\draw[blue,-]
({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
}
\draw[-latex] (\xmin-.1,0)--(\xmax+.1,0) node[below right] {$x$};
\draw[-latex] (0,\ymin-.1)--(0,\ymax+.1) node[above left] {$y$};
\end{tikzpicture}
\end{document}
答案1
綠色曲線透過旋轉從紅色曲線中顯現出來。以下可以進一步簡化,但恕我直言,您的問題寫得不是很清楚。可以用鑰匙將東西放在路徑中間midway
。
\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[declare function=
{f(\x,\y)=\x*\x-\y*\y;
g1(\x)=\x;
g2(\x)=-\x;
g3(\x)=sqrt(\x*\x+1);
g4(\x)=-sqrt(\x*\x+1);
},
scale=2.5]
\def\xmax{2.0} \def\xmin{-2.0}
\def\ymax{2.0} \def\ymin{-2.0}
\def\nx{15} \def\ny{15}
\begin{scope}[red]
\draw plot[domain=\xmin-0.1:\xmax+0.1] (\x,{g1(\x)});
\draw plot[domain=\xmin-0.1:\xmax+0.1] (\x,{g2(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
\end{scope}
\begin{scope}[green,rotate=90]
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
\end{scope}
\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
\draw[blue,-]
({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
node[midway,sloped]{$\times$};
\draw[blue,-]
({\xmin+\i*\hx},{g2(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
node[midway,sloped]{$\times$};
\draw[blue,-]
({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15)
node[midway,sloped]{$\times$};
\draw[blue,-]
({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15)
node[midway,sloped]{$\times$};
}
\begin{scope}[rotate=90]
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
\draw[blue,-]
({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
node[midway,sloped]{$\times$};
\draw[blue,-]
({\xmin+\i*\hx},{g2(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
node[midway,sloped]{$\times$};
\draw[blue,-]
({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15)
node[midway,sloped]{$\times$};
\draw[blue,-]
({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15)
node[midway,sloped]{$\times$};
}
\end{scope}
\draw[-latex] (\xmin-.1,0)--(\xmax+.1,0) node[below right] {$x$};
\draw[-latex] (0,\ymin-.1)--(0,\ymax+.1) node[above left] {$y$};
\end{tikzpicture}
\end{document}
答案2
在@user121799的幫助下,我得到了我想要的。({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
將({atan2(0,1)}:-0.075) ++ ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
斜坡中心放置在等斜線上就足夠了。也許這對某人有用。
\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[declare function=
{f(\x,\y)=\x*\x-\y*\y;
g1(\x)=\x;
g2(\x)=-\x;
g3(\x)=sqrt(\x*\x+1);
g4(\x)=-sqrt(\x*\x+1);
},
scale=2.5]
\def\xmax{2.0} \def\xmin{-2.0}
\def\ymax{2.0} \def\ymin{-2.0}
\def\nx{15} \def\ny{15}
\begin{scope}[red]
\draw plot[domain=\xmin-0.3:\xmax+0.3] (\x,{g1(\x)});
\draw plot[domain=\xmin-0.3:\xmax+0.3] (\x,{g2(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
\end{scope}
\begin{scope}[red,rotate=90]
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
\end{scope}
\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
\draw[blue,-]
({atan2(0,1)}:-0.075) ++ ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15);
\draw[blue,-]
({atan2(0,1)}:-0.075) ++ ({\xmin+\i*\hx},{g2(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15);
\draw[blue,-]
({atan2(-1,1)}:-0.075) ++ ({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
\draw[blue,-]
({atan2(-1,1)}:-0.075) ++ ({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
}
\begin{scope}[rotate=90]
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
\draw[blue,-]
({atan2(-1,1)}:-0.075) ++ ({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
\draw[blue,-]
({atan2(-1,1)}:-0.075) ++ ({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
}
\end{scope}
\draw[-latex] (\xmin-.1,0)--(\xmax+.1,0) node[below right] {$x$};
\draw[-latex] (0,\ymin-.1)--(0,\ymax+.1) node[above left] {$y$};
\end{tikzpicture}
\end{document}