绘制旋转的周围矩形

绘制旋转的周围矩形

我希望围绕节点 C_{32} 和 C_{23} 的矩形刚好包含这些节点,例如 C_{12} 和 C_{22}

我的代码如下:我已设法旋转矩形,但矩形过度拟合

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{patterns}
\tikzset{
    vertex/.style={
        fill,
        shape=circle,
        node distance=80pt},
    edge/.style={
        fill,
        opacity=.2,
        fill opacity=.5,
        line cap=round,
        line join=round,
        line width=20pt},
    elabel/.style={
        fill,
        shape=circle,
        node distance=30pt,
        opacity = .2}
}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

    \begin{tikzpicture}
    \begin{pgfonlayer}{background}

    %Task T1

    \node[vertex,label={[font = \Large]above:\(C_{11}, \$30\)}] (v1) {};

    \node[vertex,right of=v1,label={[font = \Large]above:\(C_{12}, \$10\)}] (v2) {};

    \node[vertex,right of=v2,label={[font = \Large]above:\(C_{13}, \$25\)}] (v3) {};

    \node[vertex,right of=v3,label={[font = \Large]above:\(C_{14}, \$15\)}] (v4) {};

    \node[vertex,right of=v4,label={[font = \Large]above:\(C_{15}, \$15\)}] (v5) {};


    %Task T2

    \node[vertex,below of=v1,label={[font = \Large]above:\(C_{21}\)}] (v6) {};

    \node[vertex,right of=v6,label={[font = \Large]above:\(C_{22}, \$10\)}] (v7) {};

    \node[vertex,right of=v7,label={[font = \Large]above:\(C_{23}, \$15\)}] (v8) {};

    \node[vertex,right of=v8,label={[font = \Large]above:\(C_{24}, \$20\)}] (v9) {};

    \node[vertex,right of=v9,label={[font = \Large]above:\(C_{25}, \$15\)}] (v10) {};

    %Task T3

    \node[vertex,below of=v6,label={[font = \Large]above:\(C_{31}\)}] (v11) {};

    \node[vertex,right of=v11,label={[font = \Large]above:\(C_{32}, \$15\)}] (v12) {};

    \node[vertex,right of=v12,label={[font = \Large]above:\(C_{33}, \$15\)}] (v13) {};

    \node[vertex,right of=v13,label={[font = \Large]above:\(C_{34}, \$15\)}] (v14) {};



    %Edges

    \draw[edge,color=green] (v6) -- (v11);

    \draw[edge,color=blue] (v2) -- (v7);

    \draw[edge,color=violet] (v8) -- (v12);

    \draw[edge,color=cyan] (v4) -- (v9) -- (v13);

    \draw[edge,color=orange] (v5) -- (v10) -- (v14);



    \end{pgfonlayer}

    %Labels

    \node[elabel,color=green,label=right:\(\$50\)]  (e1) at (0,-7) {};
    \node[elabel, color=blue,label=right:\(\$15\)]  (e2) at (1.5, -7) {};
    \node[elabel,color=violet,label=right:\(\$15\)]  (e3) at (3,-7) {};
    \node[elabel,color=cyan,label=right:\(\$40\)]  (e4) at (4.5, -7) {};
    \node[elabel,color=orange, label=right:\(\$30\)]  (e5) at (6.0,-7) {};


    %Task nodes

    \node[left of=v1,label={[font = \Large]above:Task \( T_1\)}, rotate = 90] (t1) {};

    \node[left of=v6,label={[font = \Large]above:Task \(T_2\)}, rotate = 90] (t2) {};

    \node[left of=v11,label={[font = \Large]above:Task \(T_3\)}, rotate = 90] (t3) {};


    %\draw (-1,1.2) rectangle (1,-9.8);

    \node [draw, fit=(v1)(v5), inner sep=28pt] {};
    \node [draw, fit=(v6)(v10), inner sep=28pt] {};
    \node [draw, fit=(v11)(v14), inner sep=28pt] {};

    %Edge Composition Constraint

    \node [draw, fit=(v2)(v7), inner sep=20pt] {};
    \node [draw, fit=(v8)(v12), rotate fit=45, inner sep = 12pt] {};    

    \end{tikzpicture}
\end{document}

渲染图像

答案1

顺序很重要。你必须rotate fit=<angle>在之前提及fit=<coordinates>

\node [draw, rotate fit=45, fit=(v8)(v12),  inner sep = 12pt] {};    

在此处输入图片描述

完整代码:

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{patterns}
\tikzset{
    vertex/.style={
        fill,
        shape=circle,
        node distance=80pt},
    edge/.style={
        fill,
        opacity=.2,
        fill opacity=.5,
        line cap=round,
        line join=round,
        line width=20pt},
    elabel/.style={
        fill,
        shape=circle,
        node distance=30pt,
        opacity = .2}
}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

    \begin{tikzpicture}
    \begin{pgfonlayer}{background}

    %Task T1

    \node[vertex,label={[font = \Large]above:\(C_{11}, \$30\)}] (v1) {};

    \node[vertex,right of=v1,label={[font = \Large]above:\(C_{12}, \$10\)}] (v2) {};

    \node[vertex,right of=v2,label={[font = \Large]above:\(C_{13}, \$25\)}] (v3) {};

    \node[vertex,right of=v3,label={[font = \Large]above:\(C_{14}, \$15\)}] (v4) {};

    \node[vertex,right of=v4,label={[font = \Large]above:\(C_{15}, \$15\)}] (v5) {};


    %Task T2

    \node[vertex,below of=v1,label={[font = \Large]above:\(C_{21}\)}] (v6) {};

    \node[vertex,right of=v6,label={[font = \Large]above:\(C_{22}, \$10\)}] (v7) {};

    \node[vertex,right of=v7,label={[font = \Large]above:\(C_{23}, \$15\)}] (v8) {};

    \node[vertex,right of=v8,label={[font = \Large]above:\(C_{24}, \$20\)}] (v9) {};

    \node[vertex,right of=v9,label={[font = \Large]above:\(C_{25}, \$15\)}] (v10) {};

    %Task T3

    \node[vertex,below of=v6,label={[font = \Large]above:\(C_{31}\)}] (v11) {};

    \node[vertex,right of=v11,label={[font = \Large]above:\(C_{32}, \$15\)}] (v12) {};

    \node[vertex,right of=v12,label={[font = \Large]above:\(C_{33}, \$15\)}] (v13) {};

    \node[vertex,right of=v13,label={[font = \Large]above:\(C_{34}, \$15\)}] (v14) {};



    %Edges

    \draw[edge,color=green] (v6) -- (v11);

    \draw[edge,color=blue] (v2) -- (v7);

    \draw[edge,color=violet] (v8) -- (v12);

    \draw[edge,color=cyan] (v4) -- (v9) -- (v13);

    \draw[edge,color=orange] (v5) -- (v10) -- (v14);



    \end{pgfonlayer}

    %Labels

    \node[elabel,color=green,label=right:\(\$50\)]  (e1) at (0,-7) {};
    \node[elabel, color=blue,label=right:\(\$15\)]  (e2) at (1.5, -7) {};
    \node[elabel,color=violet,label=right:\(\$15\)]  (e3) at (3,-7) {};
    \node[elabel,color=cyan,label=right:\(\$40\)]  (e4) at (4.5, -7) {};
    \node[elabel,color=orange, label=right:\(\$30\)]  (e5) at (6.0,-7) {};


    %Task nodes

    \node[left of=v1,label={[font = \Large]above:Task \( T_1\)}, rotate = 90] (t1) {};

    \node[left of=v6,label={[font = \Large]above:Task \(T_2\)}, rotate = 90] (t2) {};

    \node[left of=v11,label={[font = \Large]above:Task \(T_3\)}, rotate = 90] (t3) {};


    %\draw (-1,1.2) rectangle (1,-9.8);

    \node [draw, fit=(v1)(v5), inner sep=28pt] {};
    \node [draw, fit=(v6)(v10), inner sep=28pt] {};
    \node [draw, fit=(v11)(v14), inner sep=28pt] {};

    %Edge Composition Constraint

    \node [draw, fit=(v2)(v7), inner sep=20pt] {};
    \node [draw, rotate fit=45, fit=(v8)(v12),  inner sep = 12pt] {};    

    \end{tikzpicture}
\end{document}

相关内容