我想画出每个连接到圆的边。圆相当小。
这是我正在做的事情
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\tikzstyle{sq1} = [rectangle, minimum width=1cm, minimum height=1cm, text centered, draw=black]
\tikzstyle{cir} = [circle, minimum width=0.7cm, minimum height=0.7cm, text centered, draw=black ]
\tikzstyle{arrow} = [thick,->]
\tikzstyle{sqvec} = [matrix]
\begin{document}
\begin{tikzpicture}
\node [sqvec] (v6) at (2.5,4) {
\node[sq1] {}; &\node[sq1] {}; &\node[sq1] {}; &\node[sq1] {}; &\node[sq1] {};;\\
};
\node [sqvec] (v7) at (2,10) {
\node[cir] {}; \\
\node[cir] {}; \\
\node[cir] {}; \\
};
\draw (1,4.5) node (v2) {} -- (4,4.5) node (v3) {} -- (0,4.5) node (v1) {} -- (5,4.5) node (v4) {};
\draw (v1) edge (v7);
\draw (v2) edge (v7);
\draw (v3) edge (v7);
\draw (v4) edge (v7);
\end{tikzpicture}
\end{document}
答案1
我建议使用matrix of nodes
,然后您可以利用节点自动命名的事实,并在适当的锚点之间画线。
我认为一般建议使用\tikzset{stylename/.style={...}}
而不是\tikzstyle{stylename}=[...]
,因此代码发生了变化。
如果您希望圆位于矩形上方的中心,我建议添加\usetikzlibrary{positioning}
,并使用 ,\node [sqvec,nodes={cir},above=3cm of v6] (v7) ...
而不是您在代码中使用的显式坐标。在这种情况下,您还可以通过将 eg 添加xshift=-5mm
到节点选项来将圆向侧面移动,如下例所示。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\tikzset{
sq1/.style={rectangle, minimum width=1cm, minimum height=1cm, text centered, draw=black},
cir/.style={circle, minimum width=0.7cm, minimum height=0.7cm, text centered, draw=black},
arrow/.style={thick,->},
sqvec/.style={matrix,matrix of nodes,nodes in empty cells},
}
\begin{document}
\begin{tikzpicture}
\node [sqvec,column sep=-\pgflinewidth,nodes={sq1}] (v6) at (2.5,4) {
&&&&\\
};
% \node [sqvec,nodes={cir}] (v7) at (2,10) { % explicit positioning
\node [sqvec,nodes={cir},above=3cm of v6,xshift=-5mm] (v7) { % relative positioning
\\
\\
\\
};
\draw (v6-1-1.north west) -- (v7-2-1.west);
\draw (v6-1-1.north east) -- (v7-3-1.west);
\draw (v6-1-5.north east) -- (v7-2-1.east);
\draw (v6-1-5.north west) -- (v7-3-1.east);
\end{tikzpicture}
\end{document}
答案2
一种不带的替代方案matrix
,重现显示了所需的结果:
或者
母语:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{calc, positioning}
\tikzset{
sql/.style = {rectangle, draw, minimum size=12mm, outer sep=0pt, align=center},
cir/.style = {circle, draw, minimum size= 3mm, outer sep=0pt, align=center},
}
\begin{document}
% first image
\begin{tikzpicture}[node distance=0mm]
\node (s1) [sql] {};
\foreach \i [count=\j from 1] in {2,...,5}
\node (s\i) [sql,right=of s\j] {};
%
\node (c1) [cir, above=22mm of s3] {};
\node (c2) [cir, above=of c1] {};
%
\draw (s1.north west) -- (c2.west)
(s1.north east) -- (c1.west)
%
(s4.north east) -- (c1.east)
(s5.north east) -- (c2.east)
;
\end{tikzpicture}
% second image
\begin{tikzpicture}[node distance=0mm, draw=blue!25!gray]
\node (s1) [sql] {};
\foreach \i [count=\j from 1] in {2,...,5}
\node (s\i) [sql,right=of s\j] {};
%
\node (c1) [cir, above=22mm of s3] {};
\node (c2) [cir, above=of c1] {};
%
\draw (s1.north west) -- (tangent cs:node=c2,point={(s1.north west)},solution=2)
(s1.north east) -- (tangent cs:node=c1,point={(s1.north east)},solution=2)
%
(s5.north west) -- (tangent cs:node=c1,point={(s5.north west)},solution=1)
(s5.north east) -- (tangent cs:node=c2,point={(s5.north east)},solution=1)
;
\end{tikzpicture}
\end{document}
答案3
\foreach
使用循环和库tangent cs
的简化解决方案calc
。所有尺寸均可根据需要更改。
\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[line width=.7pt,outer sep=0pt]
\foreach \x in {0,...,4}
\draw (\x,0) rectangle ++(1,1) (\x,1)coordinate(p\x);
\path (2.5,4.0)coordinate(center1)
(2.5,4.7)coordinate(center2)
(2.5,5.4)coordinate(center3)
(5,1)coordinate(p5);
\foreach \n in {1,2,3}
\node [circle,draw] (c\n) at (center\n) [minimum size=.7cm] {};
\draw (p0) -- (tangent cs:node=c2,point={(p0)},solution=2)
(p1) -- (tangent cs:node=c1,point={(p1)},solution=2)
(p4) -- (tangent cs:node=c1,point={(p4)},solution=1)
(p5) -- (tangent cs:node=c2,point={(p5)},solution=1);
\end{tikzpicture}
\end{document}