我想创建一个包含较小节点的较大节点集合。我希望它们与较大节点的中心保持固定距离,这样它们就面向连接的较小节点。
我尝试使用一些可以自动执行此操作的代码,通过计算向量并对其进行规范化(并乘以范数),但由于某种原因,较小的节点最终出现在不同的半径上:
https://i.stack.imgur.com/OYpd1.png
梅威瑟:
\documentclass[aps,pra,reprint,superscriptaddress, usenames,dvipsnames]{revtex4-1}
%
\usepackage{tikz} % TikZ and PGF
\usepackage{pgfplots}
\usepackage{calc}
\def\nodexb{-0.75}%
\def\nodeyb{2.6}%
\def\nodexc{1.9}%
\def\nodeyc{0.5}%
\def\nodexf{3.5}%
\def\nodeyf{3.1}%
\def\norm{30}%
\def\dispnbfx{(\nodexb-\nodexf)}%
\def\dispnbfy{(\nodeyb-\nodeyf)}%
\def\normbf{pow(\dispnbfx*\dispnbfx+\dispnbfy*\dispnbfy, \ratio{1}{2})}%
\def\dispbfx{\ratio{\dispnbfx}{\norm*\normbf}}%
\def\dispbfy{\ratio{\dispnbfy}{\norm*\normbf}}%
\def\dispncfx{(\nodexc-\nodexf)}%
\def\dispncfy{(\nodeyc-\nodeyf)}%
\def\normcf{pow(\dispncfx*\dispncfx+\dispncfy*\dispncfy, \ratio{1}{2})}%
\def\dispcfx{\ratio{\dispncfx}{\norm*\normcf}}%
\def\dispcfy{\ratio{\dispncfy}{\norm*\normcf}}%
\begin{document}
\begin{figure}[h]
\tikzset{main node/.style={circle,fill=blue!20,draw,minimum size=1cm,inner sep=0pt},
}
\pgfmathsetmacro{\dis}{3}
\begin{tikzpicture}[main_node/.style={circle,draw,minimum size=4em]}, small_node/.style={circle, draw,minimum size=1em]}, myline/.style={line width = 0.2mm}]
\node[main_node] (nodeb) at (\nodexb, \nodeyb) {b};
\node[main_node] (nodec) at (\nodexc, \nodeyc) {c};
\node[main_node] (nodef) at (\nodexf, \nodeyf) {f};
\node[small_node] (membf) at ({\nodexb-\dispbfx}, {\nodeyb-\dispbfy}) {};
\node[small_node] (memfb) at ({\nodexf+\dispbfx}, {\nodeyf+\dispbfy}) {};
\node[small_node] (memcf) at ({\nodexc-\dispcfx}, {\nodeyc-\dispcfy}) {};
\node[small_node] (memfc) at ({\nodexf+\dispcfx}, {\nodeyf+\dispcfy}) {};
\draw[myline] (membf) -- (memfb);
\draw[myline] (memcf) -- (memfc);
\end{tikzpicture}
\end{figure}
\end{document}
答案1
您可以使用calc
库(而不是包)进行坐标计算。这样,您就不需要将坐标存储在宏中,如果您已命名坐标或节点,则可以随时提取它们。重复操作可以存储在 中pic
。
\documentclass[aps,pra,reprint,superscriptaddress, usenames,dvipsnames]{revtex4-1}
%
\usepackage{tikz} % TikZ and PGF
\usetikzlibrary{calc}
\begin{document}
\begin{figure*}
\centering
\begin{tikzpicture}[main_node/.style={circle,draw,minimum size=4em},
small_node/.style={circle, draw,minimum size=1em},
myline/.style={line width = 0.2mm},
pics/small nodes/.style={code={
\tikzset{small nodes/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/small nodes/##1}}%
\path
let \p1=($(\pv{main1})-(\pv{main2})$),\n1={atan2(\y1,\x1)} in
($(\pv{main1})+(180+\n1:\pv{d})$) node[small_node,name=\pv{name1}] {}
($(\pv{main2})+(\n1:\pv{d})$) node[small_node,name=\pv{name2}]{}
(\pv{name1}) edge[myline] (\pv{name2})
;
}},
small nodes/.cd,d/.initial=1.25em,
main1/.initial=nodeb,main2/.initial=nodec,
name1/.initial=membc,name2/.initial=memcb]
\node[main_node] (nodeb) at (-0.75, 2.6) {b};
\node[main_node] (nodec) at (1.9,0.5) {c};
\node[main_node] (nodef) at (3.5,3.1) {f};
\pic{small nodes={main1=nodeb,main2=nodec,name1=membc,name2=memcb}};
\pic{small nodes={main1=nodeb,main2=nodef,name1=membf,name2=memfb}};
\pic{small nodes={main1=nodec,main2=nodef,name1=memcf,name2=memfc}};
\end{tikzpicture}
\end{figure*}
\end{document}
小节点与中心的距离存储在pgf键中d
,主节点的名称存储在main1
和中main2
,在pic中创建的节点的名称存储在name1
和中name2
。