\documentclass[crop, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{topaths,calc, backgrounds}
\begin{document}
\begin{tikzpicture}
% Define the nodes and their positions
\node[draw,circle,fill=blue!40] (v1) at (1,0) {$v_1$};
\node[draw,circle,fill=blue!40] (v2) at (3,0) {$v_2$};
\node[draw,circle,fill=blue!40] (v3) at (2,1.732) {$v_3$};
\node[draw,circle,fill=blue!40] (v4) at (2,-1.732) {$v_4$};
% Draw the shape and fill it
\begin{pgfonlayer}{background}
\filldraw[fill=yellow!70] ($(v1) + (-0.5,0)$)
to[out=90,in=180] ($(v1) + (0.5, 0.5)$)
to[out=0,in=270] ($(v3) + (-0.5, 0)$)
to[out=90,in=180] ($(v3) + (0, 0.5)$)
to[out=0,in=90] ($(v3) + (0.5, 0.0)$)
to[out=270,in=180] ($(v2) + (-0.5, 0.5)$)
to[out=0,in=90] ($(v2) + (0.5, 0)$)
to[out=270,in=0] ($(v2) + (-0.5, -0.5)$)
to[out=180,in=90] ($(v4) + (0.5, 0.0)$)
to[out=270,in=0] ($(v4) + (0.0, -0.5)$)
to[out=180,in=270] ($(v4) + (-0.5, 0.0)$)
to[out=90,in=0] ($(v1) + (0.5, -0.5)$)
to[out=180,in=270] ($(v1) + (-0.5,0)$);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
另外,有没有更好的方法来绘制这个图形?上面的tikz
代码生成了下图。
答案1
您可以将star
节点与 一起使用rounded corners
。尝试使用minimum size
和star point ratio
直到获得所需的结果。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\tikzset{mynode/.style={draw, circle, fill=blue!40, minimum size=5mm}}
\begin{document}
\begin{tikzpicture}
\node[star, star points =4, star point ratio=1.7, draw, rounded corners=5mm, minimum size=3.2cm, fill=red!20] at (0,0) {};
\node[mynode] at (0,1){};
\node[mynode] at (1,0){};
\node[mynode] at (0,-1){};
\node[mynode] at (-1,0){};
\end{tikzpicture}
\end{document}
答案2
您发布的解决方案很好。
以下是替代方法的草图。它不一定能减少代码量,但可能会为您提供更多选择。主要思想:
使用fit
库。接下来,定义Fit
包含 4 个节点的节点,但不要绘制它。增加它的inner sep
占比,因为我们想重用它的锚点。
% ~~~ this nodes rectangle contains all 4 ~~~~~~~~~~
\node[fit=(v1) (v2) (v3) (v4), inner sep=7mm] (Fit) {};
最后,使用您喜欢的绘图选项,使用锚点(Fit.west)
等绘制路径。对于更复杂的黄色区域,您可能需要定义例如 4 个点,例如通过使用midway
和相关(参见手册)。(例如,使用 4 个锚点进行不对称控制,在 v1-v4 周围形成一个不对称形状。) controls
让您更好地控制摆动(贝塞尔曲线;是的,我简化了法语书写)。// 因为懒惰,出于演示目的,我只用绘制路径rounded corners
。
% ~~~ now draw a shape using its anchors ~~~~~
% refine this path as needed, e.g. by defining
% midway positions and use controls (Bezier) rather than angles
\draw[fill=yellow!70,rounded corners=1cm] (Fit.west) -- (Fit.north) -- (Fit.east) -- (Fit.south) -- cycle;
一旦理解了指定的 4 个点之间的滑行线,这些控制就更容易控制:
- 虚线:使用参数 t 运行
- 绿色:已连接;再次运行
- 蓝色:互连
- 曲线上的点:蓝色互连线上的 t 值
% https://tex.stackexchange.com/questions/687673/i-want-to-draw-the-attached-figure-shown-below-i-have-also-included-the-code-fo
\documentclass[crop, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{topaths,calc, backgrounds,fit}% <<<
\begin{document}
\begin{tikzpicture} % one way to do it
% Define the nodes and their positions
\node[draw,circle,fill=blue!40] (v1) at (1,0) {$v_1$};
\node[draw,circle,fill=blue!40] (v2) at (3,0) {$v_2$};
\node[draw,circle,fill=blue!40] (v3) at (2,1.732) {$v_3$};
\node[draw,circle,fill=blue!40] (v4) at (2,-1.732) {$v_4$};
\begin{pgfonlayer}{background}
% ~~~ this nodes rectangle contains all 4 ~~~~~~~~~~
\node[fit=(v1) (v2) (v3) (v4), inner sep=7mm] (Fit) {};
% ~~~ now draw a shape using its anchors ~~~~~
% refine this path as needed, e.g. by defining
% midway positions and use controls (Bezier) rather than angles
\draw[fill=yellow!70,rounded corners=1cm] (Fit.west) -- (Fit.north) -- (Fit.east) -- (Fit.south) -- cycle;
\end{pgfonlayer}
\end{tikzpicture}
\begin{tikzpicture} % posted solution
% Define the nodes and their positions
\node[draw,circle,fill=blue!40] (v1) at (1,0) {$v_1$};
\node[draw,circle,fill=blue!40] (v2) at (3,0) {$v_2$};
\node[draw,circle,fill=blue!40] (v3) at (2,1.732) {$v_3$};
\node[draw,circle,fill=blue!40] (v4) at (2,-1.732) {$v_4$};
% Draw the shape and fill it
\begin{pgfonlayer}{background}
\filldraw[fill=yellow!70] ($(v1) + (-0.5,0)$)
to[out=90,in=180] ($(v1) + (0.5, 0.5)$)
to[out=0,in=270] ($(v3) + (-0.5, 0)$)
to[out=90,in=180] ($(v3) + (0, 0.5)$)
to[out=0,in=90] ($(v3) + (0.5, 0.0)$)
to[out=270,in=180] ($(v2) + (-0.5, 0.5)$)
to[out=0,in=90] ($(v2) + (0.5, 0)$)
to[out=270,in=0] ($(v2) + (-0.5, -0.5)$)
to[out=180,in=90] ($(v4) + (0.5, 0.0)$)
to[out=270,in=0] ($(v4) + (0.0, -0.5)$)
to[out=180,in=270] ($(v4) + (-0.5, 0.0)$)
to[out=90,in=0] ($(v1) + (0.5, -0.5)$)
to[out=180,in=270] ($(v1) + (-0.5,0)$);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
附言:作为附加内容,这里有一种明确引入 4 个坐标的方法。
如代码所示,现在有 4 个参数可以改变外观:
- 参数1:极坐标的角度
- 参数2:极坐标半径
- 参数3:此节点内部分离
(Fit)
- 参数4:圆角半径
虽然我很懒,但是我用了一个\foreach
循环来定义 4 个角度和一个计数器\i
,这有助于命名坐标名称。
% ~~~ defining 4 extra points ~~~~~~~~~
\foreach \a [count=\i] in {50, 140, 230, 310}% <<< Parameter 1: angles of polar coordinates
\coordinate (c\i) at (\a:1.1);% <<<< Parameter 2: radius of polar coordinates
\begin{tikzpicture} % second way to do it
% Define the nodes and their positions
% ~~~ shifted to the left ~~~ <<<<<<<<<<<<<<<<
\node[draw,circle,fill=blue!40] (v1) at (-1,0) {$v_1$};
\node[draw,circle,fill=blue!40] (v2) at (1,0) {$v_2$};
\node[draw,circle,fill=blue!40] (v3) at (0,1.732) {$v_3$};
\node[draw,circle,fill=blue!40] (v4) at (0,-1.732) {$v_4$};
% ~~~ defining 4 extra points ~~~~~~~~~
\foreach \a [count=\i] in {50, 140, 230, 310}% <<< Parameter 1: angles of polar coordinates
\coordinate (c\i) at (\a:1.1);% <<<< Parameter 2: radius of polar coordinates
\begin{pgfonlayer}{background}
% ~~~ this nodes rectangle contains all 4 ~~~~~~~~~~
\node[fit=(v1) (v2) (v3) (v4), inner sep=13mm] (Fit) {};% <<< Parameter 3: this nodes inner separation
% ~~~ now draw a shape using its anchors ~~~~~
% refine this path as needed, e.g. by defining
% midway positions and use controls (Bezier) rather than angles
\draw[fill=yellow!70,rounded corners=10mm] % <<< Parameter 4: radius of rounded corners
(Fit.west) -- (c2) -- (Fit.north) -- (c1) --
(Fit.east) -- (c4) -- (Fit.south) -- (c3) -- cycle;
% ~~~ just to show the 4 extra coordinates ~~~
\draw[fill=orange!70] (c2) -- (c1) -- (c4) -- (c3) -- cycle;
\end{pgfonlayer}
\end{tikzpicture}