我想知道如何在手绘图片中绘制红线和蓝线到由
\begin{tikzpicture}
\clip (-1,-1) rectangle (6cm,6cm);
\end{tikzpicture}
此外,我还想在图像中添加顶点 $v$。
到目前为止,我已经能够制作以下内容:
\begin{tikzpicture}
\foreach \x in {1,...,4}
{
\foreach \y in {0,...,3}
{
\node(circ-\x-\y)[draw,circle,inner sep=1pt,fill] at (.5*\x,.5*\y) {};
}
}
% lines
\foreach \i [evaluate=\i as \j using int(\i+1)] in {2,...,4}{
\draw[red!80!black] (circ-1-0) -- (circ-\i-3);
\draw[red!80!black] (circ-1-0) -- (circ-\j-1);
}
\end{tikzpicture}
我得到了以下图像:
答案1
您已经非常接近期望的结果了:-)。使用两个循环的简单基本解决方案是:
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill, minimum size=3pt,
inner sep=0pt, outer sep=0pt}
]
\foreach \i in {1,2,3,4}
{
\foreach \j in {1,2,3,4}
\node (n\i\j) [dot] at (\i,\j) {};
}
%
\scoped[on background layer]
{
\draw[blue] (n14) |- (n41);
\draw[red] (n11) -- (n24)
(n11) -- (n23)
(n11) -- (n34)
(n11) -- (n32)
(n11) -- (n44)
(n11) -- (n43)
(n11) -- (n42);
}
\node[below left] at (n11) {$V$};
\end{tikzpicture}
\end{document}
答案2
更新
这里,ifnum
根据 TeX 语法进行测试,我们在创建节点后立即绘制红色线段。
\documentclass[tikz,border=5mm]{独立}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,3}
{\foreach \y in {0,...,3}
{\node(circ-\x-\y)[draw,circle,inner sep=1pt,fill] at (.5*\x,.5*\y) {};
% lines
\ifnum \x>0
\ifnum \y>0
\draw[red!80!black](0,0)--(.5*\x,.5*\y);
\fi\fi
}
}
\draw[blue!80!black](circ-0-0)--(circ-3-0)(circ-0-0)--(circ-0-3);
\end{tikzpicture}
\end{document}
通过这样做,我已经完成了代码中足够的循环。
\documentclass[tikz, border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,3}
{
\foreach \y in {0,...,3}
{
\node(circ-\x-\y)[draw,circle,inner sep=1pt,fill] at (.5*\x,.5*\y) {};
}
}
% lines
\draw[blue!80!black](circ-0-0)--(circ-3-0)(circ-0-0)--(circ-0-3);
\foreach \i in {1,...,3}
\foreach \j in {1,...,3}
\draw[red!80!black] (circ-0-0) -- (circ-\i-\j);
\end{tikzpicture}
\end{document}
答案3
我可以提供一个略有不同的选项。在这里,我试图避免重叠的线,即从原点到 (1,1)、从原点到 (2,2) 等。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line join=round, line cap=round]
\draw[blue](0,3) |- (3,0);
\draw[red] (0,0) -- (3,3);
\foreach \x/\y in {1/3,1/2,2/3}
{%
\draw[red] (\x,\y) -- (0,0) -- (\y,\x);
}
\foreach \x in {0,...,3} \foreach \y in {0,...,3}
{%
\fill (\x,\y) circle (1.5pt);
}
\node at (0,0) [below left] {$V$};
\end{tikzpicture}
\end{document}