使用 Tikz 绘制

使用 Tikz 绘制

我(几乎)第一次尝试使用 tikz 上的一些教程来重现此图形,但距离最终结果还很远。我该如何实现呢?这是我的代码

\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{arrows}
\begin{tikzpicture}

\draw[*-*,thick,circle](0,0) -- (4,0)  ;

\draw[-latex,thin] (0,0) -- (8,0) ;
\draw[-latex,thin] (0,0) -- (0,8)  ;

\filldraw[fill=gray!20]
  (0,0) -- (0,6) -- (2,6) -- (4,3) -- (4,0) -- (0,0);
  
\draw (2,3) node[text width=2cm,text ragged] {Feasible region};
\end{tikzpicture}
\end{document}

数字

答案1

这是一个解决方案(可以优化):

可行区域

\documentclass[tikz,border=3.14mm]{standalone}

\usetikzlibrary{arrows.meta,positioning}

\begin{document}

    \begin{tikzpicture}
    
        \tikzset{
            num/.style={draw,circle,inner sep=1pt},
            arr/.style={latex-,shorten <= 2.5pt}}
        
        \draw[fill=gray!20]
          (0,0) -- (0,6) -- (2,6) -- (4,3) -- (4,0) -- (0,0);
          
        \draw (2,3) node[text width=2cm,align=center] {Feasible\\region};
        
        \draw[latex-latex,thin] (0,7) node[left]{$x_2$} |- (7,0) node[below]{$x_1$};
        \foreach \i in {1,...,6}
            {
            \draw (\i,0) --++ (0,5pt);
            \draw (0,\i) --++ (5pt,0);
            }
        
        \fill (0,0) circle (2pt) node [num, above right = 5pt and 5pt] {0} node [left] {$(0,0)$};
        \fill (0,6) circle (2pt) node [num, below right = 5pt and 5pt] {1} node [left] {$(0,6)$};
        \fill (2,6) circle (2pt) node [num, below left = 5pt and 5pt] {2} node [above right] {$(2,6)$};
        \fill (4,3) circle (2pt) node [above right] {$(4,3)$};
        \fill (4,0) circle (2pt) node [below] {$(4,0)$};
        
        \draw[arr] (0,0) --++ (0.5,-0.5) node[right] {$Z=0$};
        \draw[arr] (0,6) --++ (0.5,0.5) node[right] {$Z=30$};
        \draw[arr] (2,6) --++ (0.5,-0.25) node[right] {$Z=36$};
        \draw[arr] (4,3) --++ (0.5,-0.5) node[right] {$Z=27$};
        \draw[arr] (4,0) --++ (0.5,0.5) node[right] {$Z=12$};
    \end{tikzpicture}
\end{document}

相关内容