你能帮我画出随机环境中的随机游动吗?我如何在网格上画出有向随机游动,例如
或者用不同的颜色。我认为方法几乎相同。但我不知道如何开始。我学会了如何绘制二叉树。
提前感谢您的帮助和时间。
答案1
TikZ 示例:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\pgfmathsetseed{3}
\begin{document}
\begin{tikzpicture}
% Coordinate axes
\begin{scope}[
semithick,
->,
>={Stealth[]},
]
\draw (0, -3.5) -- (0, 3.5);
\draw (0, 0) -- (6.5, 0);
\end{scope}
% Ticks
\draw[node font=\small]
\foreach \y in {-3, ..., 3} {
(0, \y) +(.25em, 0) -- ++(-.25em, 0)
node[left] {$\y$}
}
;
% Random walk
\draw[thick]
(0, 0)
\foreach \x in {1, ..., 6} {
-- ++(1, {2*greater(rnd, .5) - 1})
}
;
% Random points
\foreach \x in {1, ..., 6} {
\foreach \y in {-3, ..., 3} {
\pgfmathsetmacro\sy{random(0, 1)}%
\node[
circle,
fill=white,
draw,
inner sep=0pt,
]
at (\x, \y)
{$\ifcase\sy -\else +\fi$}
;
}
}
\end{tikzpicture}
\end{document}
第二个图的示例:
\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\pgfmathsetseed{3}
\begin{document}
\begin{tikzpicture}[
x=7.5mm,
y=7.5mm,
]
% Grid
\draw[thin]
\foreach \x in {0, ..., 10} {
(\x, -6) -- (\x, 6)
}
\foreach \y in {-5, ..., 5} {
(0, \y) -- (11, \y)
}
;
% Coordinate axes
\begin{scope}[
semithick,
->,
>={Stealth[]},
]
\draw (0, -6.5) -- (0, 6.5);
\draw (0, 0) -- (11.5, 0);
\end{scope}
% Random walk
\draw[thick]
(0, 0)
\foreach \x in {1, ..., 10} {
-- ++(1, {2*greater(rnd, .5) - 1})
}
;
% Random points
\tikzstyle{dot1} = [fill=white, draw];
\tikzstyle{dot2} = [fill=lightgray, draw];
\tikzstyle{dot3} = [fill=gray];
\tikzstyle{dot4} = [fill=darkgray, draw];
\tikzstyle{dot5} = [fill=black];
\foreach \x in {1, ..., 10} {
\foreach \y in {-5, ..., 5} {
\pgfmathsetmacro\sy{random(1, 5)}%
\path[dot\sy] (\x, \y) circle[radius=2mm];
}
}
\end{tikzpicture}
\end{document}