如何用TikZ绘制贴合圆形的曲线封闭空间以包围指定节点?
问题解决:用TikZ绘制贴合圆形节点的封闭虚线曲线
我想绘制红色虚线包围3个节点,形成贴合圆形轮廓的曲线封闭空间,试了不同坐标效果都不好,这是坐标设置的问题吗?
初始实现代码
\begin{tikzpicture}[auto, > = Stealth, every edge quotes/.style = {font=\footnotesize}, % 缩小边标签字体 every edge/.append style = {->, draw=cyan, thick}, every loop/.append style = {<-, looseness = 12}, node distance = 12mm, state/.style = {circle, semithick, draw=cyan, text=cyan, minimum size=1.2em}, ellipsefit/.style = {draw=red, thick, dashed, ellipse}, rect/.style = {rectangle, draw=cyan, thick, minimum width=2.5cm, minimum height=1.5cm, align=center} ]\def\radius{3} \foreach \a in {1,2,3,4,5} { \node[state] (N\a) at ({\a*360/5}:\radius) {Node \a}; } \path (N5) edge[bend right=20] (N1); \path (N1) edge[bend right=20] (N2); \path (N2) edge[bend right=20] (N3); \path (N3) edge[bend right=20] (N4); \path (N4) edge[bend right=20] (N5); \draw[red, thick, dashed] plot[smooth cycle, tension=1] coordinates { ($(N1) + (0.7, 0.7)$) ($(N2) + (-0.8, 1.5)$) ($(N3) + (0.7, -0.8)$) }; \end{tikzpicture}
初始效果:手动偏移节点坐标绘制的曲线无法贴合圆形轮廓,形状生硬,和节点所在的圆形布局不协调。
调整后的尝试代码
\draw[ultra thick,blue] (-3.9,1.9) .. controls (3.3,6.5) and (5,3) .. (-1,-2) .. controls (-1.2,-3.3) and (-3.9,-4.8) .. (-3.9,1.9);
调整后效果:曲线形状有所改善,但使用固定坐标缺乏灵活性,节点位置变化时需要重新调整所有坐标参数。
问题根源与解决方案
核心问题是手动设置偏移坐标或固定坐标没有匹配节点的圆形分布逻辑,导致曲线和整体布局脱节。推荐两种更简洁且贴合圆形的方法:
方法1:基于节点角度绘制圆弧
利用节点在半径为3的圆上的角度特性,直接计算包围圆弧的半径(比原半径稍大),绘制对应角度范围的圆弧并闭合:
\begin{tikzpicture}[auto, > = Stealth, every edge quotes/.style = {font=\footnotesize}, every edge/.append style = {->, draw=cyan, thick}, every loop/.append style = {<-, looseness = 12}, node distance = 12mm, state/.style = {circle, semithick, draw=cyan, text=cyan, minimum size=1.2em}, rect/.style = {rectangle, draw=cyan, thick, minimum width=2.5cm, minimum height=1.5cm, align=center} ]\def\radius{3} \foreach \a in {1,2,3,4,5} { \node[state] (N\a) at ({\a*360/5}:\radius) {Node \a}; } \path (N5) edge[bend right=20] (N1); \path (N1) edge[bend right=20] (N2); \path (N2) edge[bend right=20] (N3); \path (N3) edge[bend right=20] (N4); \path (N4) edge[bend right=20] (N5); % 包围N1、N2、N3的红色虚线圆弧 \def\outerradius{3.5} % 比原半径大0.5,可根据需求调整 \draw[red, thick, dashed] (72:\outerradius) arc[start angle=72, end angle=216, radius=\outerradius] % N1(72°)到N3(216°)的圆弧 -- (N3.east) -- (N2.south east) -- (N2.north west) -- (N1.south west) -- (N1.east) % 连接节点边缘,使曲线更贴合 -- cycle; \end{tikzpicture}
方法2:使用fit库拟合曲线
加载TikZ的fit库,自动根据节点位置拟合封闭曲线,通过调整内边距适配圆形轮廓:
\usetikzlibrary{fit} % 必须先加载fit库 \begin{tikzpicture}[auto, > = Stealth, every edge quotes/.style = {font=\footnotesize}, every edge/.append style = {->, draw=cyan, thick}, every loop/.append style = {<-, looseness = 12}, node distance = 12mm, state/.style = {circle, semithick, draw=cyan, text=cyan, minimum size=1.2em}, fitcircle/.style = {draw=red, thick, dashed, ellipse, inner xsep=0.8cm, inner ysep=0.8cm} % 调整内边距控制曲线与节点的距离 ]\def\radius{3} \foreach \a in {1,2,3,4,5} { \node[state] (N\a) at ({\a*360/5}:\radius) {Node \a}; } \path (N5) edge[bend right=20] (N1); \path (N1) edge[bend right=20] (N2); \path (N2) edge[bend right=20] (N3); \path (N3) edge[bend right=20] (N4); \path (N4) edge[bend right=20] (N5); % 拟合N1、N2、N3的封闭曲线 \node[fitcircle, fit=(N1) (N2) (N3)] {}; \end{tikzpicture}
关键说明
- 手动调坐标的问题在于脱离了节点的圆形分布参数(角度、半径),导致曲线和整体布局不协调;
- 方法1完全基于原圆形的参数计算,节点位置变化时只需调整
outerradius即可,灵活性极高; - 方法2用
fit库自动适配节点位置,操作简单,通过inner xsep/ysep可快速调整曲线与节点的距离。
内容的提问来源于stack exchange,提问作者Mohan Radhakrishnan
相关产品推荐
相关产品推荐

