关于可行域的一个问题

关于可行域的一个问题

我在命名由下面的 LaTeX/TikZ 代码确定的可行区域的角点时遇到了困难,谁能帮助我。

\begin{figure}[htpb]
\centering
\begin{tikzpicture}

    \draw[gray!50, thin, step=0.5] (-2,-2) grid (14,14);
    \draw[very thick,->] (-2,0) -- (14,0) node[right] {$x$};
    \draw[very thick,->] (0,-2) -- (0,14) node[above] {$y$};

   \foreach \x in {-2,...,14} \draw (\x,0.5) -- (\x,-0.5) node[below] {\tiny\x};
   \foreach \y in {-2,...,14} \draw (-0.5,\y) -- (0.5,\y) node[right] {\tiny\y};

    \fill[blue!50!cyan,opacity=0.3] (0,0)-- (0,5) -- (6,11) -- (8,10) --(13,0) --cycle;
\put(0,0){A}\put(0,5){B}\put(6,11){C}\put(8,10){D}\put(13,0){E}
    \draw (-2,3) -- node[above,sloped] {\tiny$-x+y\leq5$} (9,14);
    \draw (6,14) -- node[above,sloped] {\tiny$2x+y\leq26$} (14,-2);
    \draw (0,14) -- node[above right,sloped] {\tiny$2x+4y\leq56$} (14,7);

\end{tikzpicture} 
\caption{Feasible Region of the problem}
\label{fig:ch3:c2}
\end{figure}

答案1

正如 percusse 在他的评论中所说:\put与 TikZ 图片无关。

您可能想要使用\node(带文本)或\coordinate(以名称保存坐标)。我结合使用了两者(以及一个循环来保存 TikZ 宏和我的想法)。

首先将实际点数以名称保存 ( A, B, …):

\coordinate (<name>) at (<x>, <y>);

并借助label选项添加一个节点,以便读者可以通过名称识别点。除了选项之外label,我还可以\node添加文本。

\foreach \x/\y/\t/\p in {0/0/A/below left,
                         0/5/B/above left,
                         6/11/C/above,
                         8/10/D/below left,
                         13/0/E/above right}{% as two commands are used, those need to be
                                             % enclosed in { } so that they both are looped.
  \coordinate (\t) at (\x,\y);
  \node[\p] at (\t) {\t};
}

\fill命令中我使用了存储的坐标名称。


在第二个示例中,我使用了库intersections让 TikZ 找到正确的点。现在您可以更改线条,TikZ 会为您完成其余工作。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
  \draw[gray!50, thin, step=0.5] (-2,-2) grid (14,14);
  \draw[very thick,->] (-2,0) -- (14,0) node[right] {$x$};
  \draw[very thick,->] (0,-2) -- (0,14) node[above] {$y$};

 \foreach \x in {-2,...,14} \draw (\x,0.5) -- (\x,-0.5) node[below] {\tiny\x};
 \foreach \y in {-2,...,14} \draw (-0.5,\y) -- (0.5,\y) node[right] {\tiny\y};

  \foreach \x/\y/\t/\p in {0/0/A/below left,
                           0/5/B/above left,
                           6/11/C/above,
                           8/10/D/below left,
                           13/0/E/above right}
    \coordinate[label={\p:\t}] (\t) at (\x,\y);
  \fill[blue!50!cyan,opacity=0.3] (A) -- (B) -- (C) -- (D) -- (E) -- cycle;

  \draw (-2,3) -- node[above,sloped]       {\tiny$-x+y\leq5$}   (9,14);
  \draw (6,14) -- node[above,sloped]       {\tiny$2x+y\leq26$} (14,-2);
  \draw (0,14) -- node[above right,sloped] {\tiny$2x+4y\leq56$} (14,7);
\end{tikzpicture} 

\begin{tikzpicture}
  \draw[gray!50, thin, step=0.5] (-2,-2) grid (14,14);
  \draw[very thick,->, name path=x axis] (-2,0) -- (14,0) node[right] {$x$};
  \draw[very thick,->, name path=y axis] (0,-2) -- (0,14) node[above] {$y$};

  \foreach \x in {-2,...,14} \draw (\x,0.5) -- (\x,-0.5) node[below] {\tiny\x};
  \foreach \y in {-2,...,14} \draw (-0.5,\y) -- (0.5,\y) node[right] {\tiny\y};

  \draw[name path=line 1] (-2,3) -- node[above,sloped]       {\tiny$-x+y\leq5$}   (9,14);
  \draw[name path=line 2] (6,14) -- node[above,sloped]       {\tiny$2x+y\leq26$} (14,-2);
  \draw[name path=line 3] (0,14) -- node[above right,sloped] {\tiny$2x+4y\leq56$} (14,7);

  \tikzset{
    name intersections={of=y axis and x axis, name=A},
    name intersections={of=y axis and line 1, name=B},
    name intersections={of=line 1 and line 3, name=C},
    name intersections={of=line 3 and line 2, name=D},
    name intersections={of=x axis and line 3, name=E},
  }
  \foreach \t/\p in {A/below left,
                     B/above left,
                     C/above,
                     D/below left,
                     E/above right}
     \node[fill,circle,minimum size=6\pgflinewidth,inner sep=0pt,label={\p:\t}] at (\t) {};
  \fill[blue!50!cyan,opacity=0.3] (A) -- (B) -- (C) -- (D) -- (E) -- cycle;
\end{tikzpicture} 
\end{document}

输出

在此处输入图片描述

在此处输入图片描述

相关内容